标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 60627 Accepted Submission(s): 25703
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 #include <queue> 8 #include <map> 9 #include <stack> 10 #include <list> 11 #include <vector> 12 13 using namespace std; 14 15 const int maxn = 100000; 16 typedef struct Node { 17 int rr; 18 int ll; 19 int value; 20 }Node; 21 22 Node node[maxn*2]; 23 int peo[maxn], n, res; 24 25 void make(int ll, int rr, int num) { 26 node[num].ll = ll; 27 node[num].rr = rr; 28 if(ll == rr) { 29 node[num].value = peo[ll]; 30 return ; 31 } 32 make(ll, (ll+rr)>>1, num<<1); 33 make(((ll+rr)>>1)+1, rr, (num<<1)+1); 34 node[num].value = node[num<<1].value + node[(num<<1)+1].value; 35 } 36 37 void query(int ll, int rr, int num) { 38 if(ll <= node[num].ll && rr >= node[num].rr) { 39 res += node[num].value; 40 } 41 else { 42 int mm = (node[num].ll + node[num].rr) >> 1; 43 if(ll > mm) { 44 query(ll, rr, (num<<1)+1); 45 } 46 else if(rr <= mm) { 47 query(ll, rr, (num<<1)); 48 } 49 else { 50 query(ll, rr, num<<1); 51 query(ll, rr, (num<<1)+1); 52 } 53 } 54 } 55 56 void add(int ll, int rr, int num) { 57 node[num].value += rr; 58 if(node[num].ll == ll && node[num].rr == ll) { 59 return ; 60 } 61 if(ll > ((node[num].ll + node[num].rr)>>1)) { 62 add(ll, rr, (num<<1)+1); 63 } 64 else { 65 add(ll, rr, (num<<1)); 66 } 67 } 68 69 void sub(int ll, int rr, int num) { 70 node[num].value -= rr; 71 if(node[num].ll == ll && node[num].rr == ll) { 72 return ; 73 } 74 if(ll > ((node[num].ll + node[num].rr)>>1)) { 75 sub(ll, rr, (num<<1)+1); 76 } 77 else { 78 sub(ll, rr, num<<1); 79 } 80 } 81 82 int main() { 83 // freopen("in", "r", stdin); 84 int T; 85 int kase = 1; 86 char cmd[6]; 87 scanf("%d", &T); 88 while(T--) { 89 int ll, rr; 90 memset(cmd, 0, sizeof(cmd)); 91 scanf("%d", &n); 92 for(int i = 1; i <= n; i++) { 93 scanf("%d", &peo[i]); 94 } 95 make(1, n, 1); 96 printf("Case %d:\n", kase++); 97 while(scanf("%s", cmd) && strcmp(cmd, "End") != 0) { 98 if(strcmp(cmd, "Query") == 0) { 99 scanf("%d %d", &ll, &rr); 100 res = 0; 101 query(ll, rr, 1); 102 printf("%d\n", res); 103 } 104 else if(strcmp(cmd, "Add") == 0) { 105 scanf("%d %d", &ll, &rr); 106 add(ll, rr, 1); 107 } 108 else if(strcmp(cmd, "Sub") == 0) { 109 scanf("%d %d", &ll, &rr); 110 sub(ll, rr, 1); 111 } 112 } 113 } 114 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4770055.html