hdu 1166 敌兵布阵
2012-07-13 23:51
204 查看
这题用了自顶向下的递归方式。
#include <iostream> #include <cstring> using namespace std; const int MAXD = 50005; int tree[4*MAXD]; int a[MAXD],D; int query(int x,int y) { int i=D+x-1, j=D+y+1, ans=0; for(; i+1 != j; i>>=1,j>>=1) { if(~i & 1) ans += tree[i^1]; if(j & 1) ans += tree[j^1]; } return ans; } void update(int n) { for(; n ^ 1; n >>= 1) tree[n>>1] = tree +tree[n^1]; } int main() { int T,n,m,i,x,y; char cmd[7]; cin>>T; m = 1; while(T--) { cin>>n; for(i = 1; i <= n; i++) cin>>a[i]; D = 1; while(D < n+2) D <<= 1; memset(tree,0,sizeof(tree)); for(i = 1; i <= n; i++) tree[D+i] = a[i]; for(i = D-1; i; i--) tree[i] = tree[i<<1] + tree[i<<1|1]; cout<<"Case "<<m++<<":\n"; cin>>cmd; while(cmd[0] != 'E') { cin>>x>>y; if(cmd[0] == 'Q') cout<<query(x,y)<<endl; else if(cmd[0] == 'A') { tree[D+x] += y; update(D+x); } else if(cmd[0] == 'S') { tree[D+x] -= y; update(D+x); } cin>>cmd; } } return 0; }
相关文章推荐
- HDU 1166 敌兵布阵 【线段树】
- HDU 1166 敌兵布阵(树状数组)
- 线段树 hdu 1166 敌兵布阵 单点更新区间求和
- hdu 1166 敌兵布阵(线段树单点更新(a位置的值+/-b)+区间求和)
- hdu_1166 敌兵布阵(线段树)
- HDU 1166 敌兵布阵
- hdu 1166敌兵布阵 线段树 树状数组
- hdu 1166 敌兵布阵 线段树&树状数组
- hdu 1166敌兵布阵
- hdu 1166 敌兵布阵
- HDU 1166 敌兵布阵[线段树单点更新成段查询]
- hdu 1166 敌兵布阵 (树状数组)
- A - 敌兵布阵 - hdu 1166
- HDU 1166 敌兵布阵(splay区间求和模板)
- HDU 1166 - 敌兵布阵 - Segment Tree
- HDU 1166 敌兵布阵 Segment Tree题解
- 【树状数组初探第二弹】HDU 1166题解——敌兵布阵
- hdu 1166 敌兵布阵(线段树-单点更新)
- HDU 1166 敌兵布阵
- hdu 1166 敌兵布阵(树状数组 | 线段树)