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
| #include <bits/stdc++.h> #define ll long long using namespace std; const int N = 50000500; const int L = 1000000000; int n, m, a[N], cnt; int root[N], ch[N][2]; ll sum[N]; inline void I (int pre, int now, int l, int r, int val) { ch[now][0] = ch[pre][0], ch[now][1] = ch[pre][1]; int mid = (l + r) >> 1; sum[now] = sum[pre] + val; if(l == r) return ; if(val <= mid) ch[now][0] = ++cnt, I(ch[pre][0], ch[now][0], l, mid, val); else ch[now][1] = ++cnt, I(ch[pre][1], ch[now][1], mid + 1, r, val); } inline int Q(int pre, int now, int l, int r, int val) { if(l == r) return sum[now] - sum[pre]; int mid = (l + r) / 2; if(val <= mid) return Q(ch[pre][0], ch[now][0], l, mid, val); else return sum[ch[now][0]] - sum[ch[pre][0]] + Q(ch[pre][1], ch[now][1], mid + 1, r, val); } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); root[i] = ++cnt; I(root[i - 1], root[i], 1, L, a[i]); } scanf("%d", &m); for(int i = 1; i <= m; i++) { int l, r; scanf("%d %d", &l, &r); int ans = 1; int S; while(1) { S = Q(root[l - 1], root[r], 1, L, ans); if(S < ans) { printf("%d\n", ans); break ; } else ans = S + 1; } } return 0; }
|