1. 程式人生 > >hdu1166 敵兵布陣

hdu1166 敵兵布陣

自己 AR read %d bre n) ring stream IT

似乎當初就是學到這裏然後自己就去亂搞了?然後就棄坑了?想想挺難受的其實

樹狀數組板子題。初始化wa了一發

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 #define mem(a,b) memset(a,b,sizeof(a))
10
#define ll long long 11 #define inf 1000000000 12 #define maxn 40000 13 #define eps 1e-12 14 #define mod 1000000007 15 inline int read() 16 { 17 int x=0,f=1;char ch=getchar(); 18 while(ch<0||ch>9) {if(ch==-) f=-1;ch=getchar();} 19 while(ch>=0&&ch<=9) {x=10*x+ch-0;ch=getchar();}
20 return x*f; 21 } 22 int a[50005],n; 23 char s[20]; 24 int lowbit(int i) 25 { 26 return i&(-i); 27 } 28 void update(int i,int peo) 29 { 30 while(i<=n) 31 { 32 a[i]+=peo;i+=lowbit(i); 33 } 34 } 35 int sum(int i) 36 { 37 int sum=0; 38 while(i>0
) 39 { 40 sum+=a[i]; 41 i-=lowbit(i); 42 } 43 return sum; 44 } 45 int main() 46 { 47 int T; 48 T=read(); 49 int num=1,peo; 50 while(T--) 51 { 52 n=read(); 53 mem(a,0); 54 for(int i=1;i<=n;++i) peo=read(),update(i,peo); 55 printf("Case %d:\n",num++); 56 while(scanf("%s",s)) 57 { 58 if(s[0]==E) break; 59 int x,y; 60 x=read();y=read(); 61 if(s[0]==Q) printf("%d\n",sum(y)-sum(x-1)); 62 else if(s[0]==A) update(x,y); 63 else update(x,-y); 64 } 65 } 66 return 0; 67 }

hdu1166 敵兵布陣