敵兵佈陣(hdu 1166) (樹狀陣列模板題)
阿新 • • 發佈:2019-02-14
樹狀陣列&線段樹都可以,但是比較一下發現此題更適合用樹狀陣列來做……(第一個是樹狀陣列,第二個是線段樹)
#include<bits/stdc++.h> using namespace std; int tree[50005],n; void add(int k,int num) { while(k<=n) { tree[k]+=num; k+=k&-k;//lowbit操作 } } int read(int k) //1~k的區間和 { int sum=0; while(k) { sum+=tree[k]; k-=k&-k; } return sum; } int main() { int t; scanf("%d",&t); for(int k=1;k<=t;k++) { memset(tree,0,sizeof(tree)); int a,x,y; char s[6]; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a); add(i,a); } printf("Case %d:\n",k); while(scanf("%s",s)==1) { if(s[0]=='E') break; if(s[0]=='Q') { scanf("%d%d",&x,&y); printf("%d\n",read(y)-read(x-1)); } else if(s[0]=='A') { scanf("%d%d",&x,&y); add(x,y); } else if(s[0]=='S') { scanf("%d%d",&x,&y); add(x,-y); } } } return 0; }