pushup麻烦,查询也麻烦。。。但开个结构体保存节点就能让一切变得简单一些

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = 5e5 + 10;
int ks, n, m;
ll data[maxn], sum[maxn];
struct Node {
int pre, suf;
P ans;
}node[maxn<<2];
ll SUM(P x) { return sum[x.second] - sum[x.first-1]; }
P MAX(P x, P y) {
ll sumx = SUM(x), sumy = SUM(y);
if (sumx != sumy) return sumx > sumy ? x : y;
return x < y ? x : y;
}
void build(int l, int r, int root) {
if (l == r) {
node[root].pre = node[root].suf = l;
node[root].ans = P(l, l);
return;
}
int mid = (l + r)>>1;
build(l, mid, root<<1);
build(mid+1, r, root<<1|1);
node[root].pre = MAX(P(l, node[root<<1].pre), P(l, node[root<<1|1].pre)).second;
node[root].suf = MAX(P(node[root<<1|1].suf, r), P(node[root<<1].suf, r)).first;
node[root].ans = MAX(MAX(node[root<<1].ans, node[root<<1|1].ans), P(node[root<<1].suf, node[root<<1|1].pre));
}
int ql, qr;
Node query(int l, int r, int root) {
if (ql <= l && r <= qr) return node[root];
int mid = (l + r)>>1;
if (qr <= mid) return query(l, mid, root<<1);
else if (ql > mid) return query(mid+1, r, root<<1|1);
else {
Node ans, x = query(l, mid, root<<1), y = query(mid+1, r, root<<1|1);
ans.pre = MAX(P(l, x.pre), P(l, y.pre)).second;
ans.suf = MAX(P(y.suf, r), P(x.suf, r)).first;
ans.ans = MAX(x.ans, MAX(y.ans, P(x.suf, y.pre)));
return ans;
}
}
int main() {
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) scanf("%lld", &data[i]), sum[i] = sum[i-1] + data[i];
printf("Case %d:\n", ++ks);
build(1, n, 1);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &ql, &qr);
Node ans = query(1, n, 1);
printf("%d %d\n", ans.ans.first, ans.ans.second);
}
}
return 0;
}