标签:miss mission 情况 海岸线 main build define 线段树 fine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 89964 Accepted Submission(s):
37885
1 #include<cstdio> 2 #define lson l,m,rt<<1 3 #define rson m+1,r,rt<<1|1 4 #define MAXN 50100 5 6 int sum[MAXN<<1]; 7 int t,n; 8 char s[10]; 9 void putup(int rt) 10 { 11 sum[rt] = sum[rt<<1]+sum[rt<<1|1]; 12 } 13 void build(int l,int r,int rt) 14 { 15 if (l==r) 16 { 17 scanf("%d",&sum[rt]); 18 return ; 19 } 20 int m = (l+r)/2; 21 build(lson); 22 build(rson); 23 putup(rt); 24 } 25 void update(int l,int r,int rt,int p,int add) 26 { 27 if (l==r) 28 { 29 sum[rt] += add; 30 return ; 31 } 32 int m = (l+r)/2; 33 if (p<=m) update(lson,p,add); 34 else update(rson,p,add); 35 putup(rt); 36 } 37 int query(int l,int r,int rt,int L,int R) 38 { 39 if (L<=l && r<=R) 40 { 41 return sum[rt]; 42 } 43 int m = (l+r)/2; 44 int ret = 0; 45 if (L<=m) ret += query(lson,L,R); 46 if (R>m) ret += query(rson,L,R); 47 return ret; 48 } 49 int main() 50 { 51 scanf("%d",&t); 52 for (int i=1; i<=t; ++i) 53 { 54 scanf("%d",&n); 55 build (1,n,1); 56 printf("Case %d:\n"); 57 while (scanf("%s",s)!=EOF) 58 { 59 if (s[0]==‘E‘) break; 60 int x,y; 61 scanf("%d%d",&x,&y); 62 if (s[0]==‘A‘) update(1,n,1,x,y); 63 else if (s[0]==‘S‘) update(1,n,1,x,-y); 64 else if (s[0]==‘Q‘) printf("%d\n",query(1,n,1,x,y)); 65 } 66 } 67 return 0; 68 }
标签:miss mission 情况 海岸线 main build define 线段树 fine
原文地址:http://www.cnblogs.com/mjtcn/p/7052912.html