1. 程式人生 > >HDU1166敵兵布陣【線段樹入門】

HDU1166敵兵布陣【線段樹入門】

sum char mes sca 敵兵布陣 bsp cas i++ 線段樹

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn = 50010;
 6 int sum[maxn];
 7 int t,n;
 8 void update(int pos,int val)
 9 {
10     while(pos<=n)
11     {
12         sum[pos] += val;
13         pos += (pos&(-pos));
14 } 15 } 16 int Query(int pos) 17 { 18 int rec = 0; 19 while(pos > 0) 20 { 21 rec += sum[pos]; 22 pos -= (pos&(-pos)); 23 } 24 return rec; 25 } 26 int main(void) 27 { 28 int x; 29 scanf("%d",&t); 30 int kase = 0; 31 while
(t--) 32 { 33 memset(sum,0,sizeof(sum)); 34 cin>>n; 35 for(int i = 1; i <= n; i++) 36 { 37 cin>>x; 38 update(i,x); 39 } 40 char s[10]; 41 printf("Case %d:\n",++kase); 42 while(scanf("%s
",s)) 43 { 44 if(s[0]==E) 45 break; 46 if(s[0]==A) 47 { 48 int i,j; 49 cin>>i>>j; 50 update(i,j); 51 } 52 else if(s[0]==S) 53 { 54 int i,j; 55 cin>>i>>j; 56 update(i,-j); 57 } 58 else 59 { 60 int i,j; 61 cin>>i>>j; 62 cout<<Query(j)-Query(i-1)<<endl; 63 } 64 } 65 } 66 return 0; 67 }

HDU1166敵兵布陣【線段樹入門】