您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: