#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5+5, M = 5e5+5, P = 1e9+7;
int pre[N], fa[N], s[N], h[N], val[N], res[M], pow2[N], a[N];
struct Qry {
int l, r, id;
bool operator<(const Qry&t)const {
return r < t.r;
}
} q[M];
int qry(int l, int r) {
return (h[l] - h[r + 1] * pow2[r + 1 - l] % P + P) % P;
}
void merge(int x, int y) { // x: new br y: previous bl-1 & now in this block
pre[x] = pre[y], fa[y] = x;
if ((y - pre[y] > 30 && val[x] > 0) || val[y] + (val[x] << (y - pre[y])) >= P)val[x] = P;
else val[x] = val[y] + (val[x] << (y - pre[y]));
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
signed main() {
ios :: sync_with_stdio(0);
cin.tie(0), cout.tie(0);
pow2[0] = 1;
for (int i = 1; i < N; ++i)pow2[i] = pow2[i - 1] * 2 % P;
int n, Q;
cin >> n >> Q;
for (int i = 1; i <= n; ++i)cin >> a[i], pre[i] = i - 1, fa[i] = i, val[i] = a[i];
for (int i = n; i >= 1; --i)h[i] = (h[i + 1] * 2 % P + a[i]) % P;
for (int i = 1; i <= Q; ++i)cin >> q[i].l >> q[i].r, q[i].id = i;
sort(q + 1, q + Q + 1);
int j = 1;
for (int i = 1; i <= n; ++i) {
while (pre[i] != 0 && val[i] >= 0)merge(i, pre[i]);
s[i] = (s[pre[i]] + 2 * qry(pre[i] + 1, i) % P) % P;
while (q[j].r == i) {
int l = q[j].l, br = find(l);
res[q[j].id] = ((s[i] - s[br] + P) % P + qry(l, br)) % P;
++j;
if (j == Q + 1)break;
}
}
for (int i = 1; i <= Q; ++i)cout << res[i] << '\n';
return 0;
}
模拟赛出到了这题,赛时就写挂了,这是改过一个错误后的版本。很短的代码,但是找不到问题。