标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 62483 Accepted Submission(s): 26386
/* ID: LinKArftc PROG: 1166.cpp LANG: C++ */ #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <string> #include <utility> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const double e = exp(1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 50010; int tree[maxn << 2]; int num[maxn]; int pos[maxn]; int n; char str[50]; void build(int rt, int l, int r) { if (l == r) { tree[rt] = num[l]; pos[l] = rt; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); tree[rt] = tree[rt << 1] + tree[rt << 1 | 1]; } int query(int rt, int l, int r, int L, int R) { if (L <= l && R >= r) return tree[rt]; int mid = (l + r) >> 1; if (R <= mid) return query(rt << 1, l, mid, L, R); if (L > mid) return query(rt << 1 | 1, mid + 1, r, L, R); return query(rt << 1, l, mid, L, R) + query(rt << 1 | 1, mid + 1, r, L, R); } int main() { int T, _t = 1, a, b; scanf("%d", &T); while (T --) { printf("Case %d:\n", _t ++); scanf("%d", &n); for (int i = 1; i <= n; i ++) scanf("%d", &num[i]); build(1, 1, n); while (1) { scanf("%s", str); if (strcmp(str, "End") == 0) break; scanf("%d %d", &a, &b); if (strcmp(str, "Query") == 0) printf("%d\n", query(1, 1, n, a, b)); else if (strcmp(str, "Add") == 0) { int rt = pos[a]; num[a] += b; while (rt) { tree[rt] += b; rt >>= 1; } } else if (strcmp(str, "Sub") == 0) { int rt = pos[a]; num[a] -= b; while (rt) { tree[rt] -= b; rt >>= 1; } } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/LinKArftc/p/4907858.html