标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40159 Accepted Submission(s): 16960
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define ls l, m, rt << 1 9 #define rs m + 1, r, rt << 1 | 1 10 11 const int MAX = 50005; 12 int N; 13 int sum[4 * MAX]; 14 15 void push_up(int rt) { 16 sum[rt] = sum[rt << 1] + sum[rt << 1|1]; 17 } 18 19 void build(int l, int r, int rt) { 20 if (l == r) { 21 scanf("%d", &sum[rt]); 22 return; 23 } 24 int m = (l + r) >> 1; 25 build(ls); 26 build(rs); 27 push_up(rt); 28 } 29 30 31 void update(int p, int add, int l, int r, int rt) { 32 int m = (l + r) >> 1; 33 if (l == r) { 34 sum[rt] += add; 35 return; 36 } 37 if (p <= m) update(p, add, ls); 38 else update(p, add, rs); 39 push_up(rt); 40 } 41 42 int query(int ql, int qr, int l, int r, int rt) { 43 int m = (l + r) >> 1; 44 if (ql <= l && r <= qr) { 45 return sum[rt]; 46 } 47 int ret = 0; 48 if (ql <= m) ret += query(ql, qr, ls); 49 if (qr > m) ret += query(ql, qr, rs); 50 return ret; 51 } 52 53 int main() 54 { 55 int t, ca = 1; 56 freopen("sw.in", "r", stdin); 57 scanf("%d", &t); 58 while (t--) { 59 scanf("%d", &N); 60 memset(sum, 0, sizeof(sum)); 61 build(1, N, 1); 62 char op[10]; 63 printf("Case %d:\n", ca++); 64 scanf("%s", op); 65 while (strcmp(op, "End") != 0) { 66 int a, b; 67 scanf("%d%d", &a, &b); 68 if (strcmp(op, "Add") == 0) { 69 update(a, b, 1, N, 1); 70 } 71 if (strcmp(op, "Sub") == 0) { 72 update(a, -b, 1, N, 1); 73 } 74 if (strcmp(op, "Query") == 0) { 75 printf("%d\n", query(a, b, 1, N, 1)); 76 } 77 scanf("%s", op); 78 } 79 80 } 81 //cout << "Hello world!" << endl; 82 return 0; 83 }
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 int N; 9 const int MAX = 50005; 10 int sum[MAX]; 11 12 int lowbit(int x) { 13 return x & (-x); 14 } 15 16 void add(int x, int d) { 17 while (x <= N) { 18 sum[x] += d; 19 x += lowbit(x); 20 } 21 } 22 23 int Sum(int x) { 24 int ret = 0; 25 while (x > 0) { 26 ret += sum[x]; 27 x -= lowbit(x); 28 } 29 return ret; 30 } 31 32 33 int main() { 34 freopen("sw.in", "r", stdin); 35 int t, ca = 1; 36 scanf("%d", &t); 37 while (t--) { 38 scanf("%d", &N); 39 memset(sum, 0, sizeof(sum)); 40 for (int i = 1; i <= N; ++i) { 41 int ch; 42 scanf("%d", &ch); 43 add(i, ch); 44 } 45 46 printf("Case %d:\n", ca++); 47 char op[10]; 48 scanf("%s", op); 49 int a, b; 50 51 while (strcmp(op, "End") != 0) { 52 scanf("%d%d", &a, &b); 53 if (strcmp(op, "Add") == 0) { 54 add(a, b); 55 } else if (strcmp(op, "Sub") == 0) { 56 add(a, -b); 57 } else { 58 printf("%d\n", Sum(b) - Sum(a - 1)); 59 } 60 scanf("%s", op); 61 } 62 } 63 64 return 0; 65 }
标签:des style blog http java color
原文地址:http://www.cnblogs.com/hyxsolitude/p/3845138.html