1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
typedef pair<int, int> PII; const int maxn = 500000 + 2; llong sum[maxn]; int n, m;
void init() { Set(sum, 0); }
llong cal(const PII x) { return sum[x.second] - sum[x.first - 1]; }
PII better(const PII& lhs, const PII& rhs) { llong suml = cal(lhs), sumr = cal(rhs); if(suml != sumr) return suml > sumr ? lhs : rhs; return lhs < rhs ? lhs : rhs; }
class SegTree { public: int l, r;
PII dat; int lx, rx; };
SegTree t[maxn * 4]; typedef SegTree Node;
Node combine(const Node& lhs, const Node& rhs) { Node ret; ret.l = lhs.l; ret.r = rhs.r;
ret.lx = better(PII(lhs.l, lhs.lx), PII(lhs.l, rhs.lx)).second; ret.rx = better(PII(rhs.rx, rhs.r), PII(lhs.rx, rhs.r)).first; // then ret.dat = cross ret.dat = better(better(lhs.dat, rhs.dat), PII(lhs.rx, rhs.lx));
return ret; }
void build(int p, int l, int r) { t[p].l = l; t[p].r = r;
if(l == r) { t[p].lx = t[p].rx = l; t[p].dat = PII(l, l); return; }
int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r);
t[p].lx = better(PII(t[p << 1].l, t[p << 1].lx), PII(t[p << 1].l, t[p << 1 | 1].lx)).second; t[p].rx = better(PII(t[p << 1 | 1].rx, t[p << 1 | 1].r), PII(t[p << 1].rx, t[p << 1 | 1].r)).first; t[p].dat = better(better(t[p << 1].dat, t[p << 1 | 1].dat), PII(t[p << 1].rx, t[p << 1 | 1].lx)); }
Node ask(int p, int x, int y) { if(x <= t[p].l && t[p].r <= y) return t[p];
int mid = (t[p].l + t[p].r) >> 1; Node ans;
if(x <= mid && mid < y) { // both left tree and right tree ans = combine(ask(p << 1, x, y), ask(p << 1 | 1, x, y)); } else if(x <= mid) { // only left ans = ask(p << 1, x, y); } else if(mid < y) { ans = ask(p << 1 | 1, x, y); }
return ans; }
int main() { freopen("input.txt", "r", stdin); int kase = 1; while (scanf("%d%d", &n, &m) != EOF) { init(); _rep(i, 1, n) { llong x; scanf("%lld", &x); sum[i] = sum[i - 1] + x; }
build(1, 1, n); printf("Case %d:\n", kase++);
_rep(i, 1, m) { int x, y; scanf("%d%d", &x, &y); PII res = ask(1, x, y).dat; printf("%d %d\n", res.first, res.second); } } }
|