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
| #include <bits/stdc++.h> #define ll long long #define IOS {ios::sync_with_stdio(false);cin.tie(0);} using namespace std;
const int maxn = 50000 + 5; ll T, N, a[maxn], d[maxn << 2]; int _a, _b; void push_up(int p) { d[p] = d[p << 1] + d[p << 1 | 1]; } void build(int l, int r, int p) { if(l == r) d[p] = a[l]; else { int m = (l + r) >> 1; build(l, m, p << 1); build(m + 1, r, p << 1 | 1); push_up(p); } } void update(int x, int v, int l, int r, int p) { if(l == r) d[p] += v; else { int m = (l + r) >> 1; if(x <= m) update(x, v, l, m, p << 1); else update(x, v, m + 1, r, p << 1 | 1); push_up(p); } } int query(int L, int R, int l, int r, int p) { if(L <= l && R >= r) return d[p]; int ans = 0; int m = (l + r) >> 1; if(L <= m) { ans += query(L, R, l, m, p << 1); } if(R > m) { ans += query(L, R, m + 1, r, p << 1 | 1); } return ans; } int main(void) { IOS char str[10]; cin >> T; for(int Case = 1; Case <= T; Case++) { memset(a, 0, sizeof(a)); memset(d, 0, sizeof(d)); cout << "Case " << Case << ":" << endl; cin >> N; for(int i = 1; i <= N; i++) cin >> a[i]; build(1, N, 1); while(cin >> str && str[0] != 'E') { cin >> _a >> _b; if(str[0] == 'Q') cout << query(_a, _b, 1, N, 1) << endl; else if(str[0] == 'A') update(_a, _b, 1, N, 1); else if(str[0] == 'S') update(_a, -_b, 1, N, 1); } } return 0; }
|