标签:bsp blog push eof pre color 打电话 otto ios
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 87684 Accepted Submission(s): 36912
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 const int maxn = 55555; 9 int sum[maxn << 2];//按位左偏一位相当于乘以2,偏移2位相当于乘以2的平方 10 void PushUp(int rt) 11 { 12 sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];//这个按位或就是+1的意思 13 } 14 15 void build(int l, int r, int rt)//建立线段树 16 { 17 if (l == r) 18 { 19 scanf("%d", &sum[rt]); 20 return; 21 } 22 int m = (l + r) >> 1;//按位右偏1位相当于除以2 23 build(l, m, rt << 1); 24 build(m + 1, r, rt << 1 | 1); 25 PushUp(rt); 26 } 27 28 void update(int p, int add, int l, int r, int rt) 29 { 30 if (l == r) 31 { 32 sum[rt] += add; 33 return; 34 } 35 int m = (l + r) >> 1; 36 if (p <= m) update(p, add, l, m, rt << 1); 37 else update(p, add, m + 1, r, rt << 1 | 1); 38 PushUp(rt); 39 } 40 41 int query(int ll, int rr, int l, int r, int rt)//查询线段树 42 { 43 if (ll <= l && rr >= r) return sum[rt]; 44 int m = (l + r) >> 1; 45 int ret = 0; 46 if (ll <= m) ret += query(ll, rr, l, m, rt << 1); 47 if (rr > m) ret += query(ll, rr, m + 1, r, rt << 1 | 1); 48 return ret; 49 } 50 51 int main() 52 { 53 int t, c; 54 char d[10]; 55 scanf("%d", &t); 56 for (c = 1; c <= t; c++) 57 { 58 printf("Case %d:\n", c); 59 int n; 60 scanf("%d", &n); 61 build(1, n, 1); 62 while (scanf("%s", d) != EOF) 63 { 64 if (d[0] == ‘E‘) break; 65 int x, y; 66 scanf("%d%d", &x, &y); 67 if (d[0] == ‘Q‘) 68 { 69 int ans = query(x, y, 1, n, 1); 70 printf("%d\n", ans); 71 } 72 if (d[0] == ‘S‘) update(x, -y, 1, n, 1); 73 if (d[0] == ‘A‘) update(x, y, 1, n, 1); 74 } 75 } 76 return 0; 77 }
标签:bsp blog push eof pre color 打电话 otto ios
原文地址:http://www.cnblogs.com/ECJTUACM-873284962/p/6786619.html