首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hdu 1166敌兵布阵(线段树也许树状数组)

2013-10-07 
hdu 1166敌兵布阵(线段树或者树状数组)http://acm.hdu.edu.cn/showproblem.php?pid1166不懂得可以看刘汝

hdu 1166敌兵布阵(线段树或者树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=1166

不懂得可以看刘汝佳训练指南P194

//树状数组版:#include <iostream>#include <cstdio> #include <cstring>using namespace std;int n,s[50005];int LowBit(int x){    return x&-x;  }void Add(int x,int d){    while(x<=n)    {  s[x]+=d;  x+=LowBit(x);}}int Sum(int x){    int ans=0;    while(x>0)    {    ans+=s[x];  x-=LowBit(x); }    return ans;}int main(int argc, char *argv[]){    int t,i,d,k=1; char a[10];    scanf("%d",&t);    while(t--!=0)    {        printf("Case %d:\n",k++);            scanf("%d",&n);            memset(s,0,sizeof(s));            for(i=1;i<=n;i++)            {                scanf("%d",&d); Add(i,d);            }                while(scanf("%s",a))            {                if(a[0]=='E') break;                scanf("%d%d",&i,&d);                if(a[0]=='Q') printf("%d\n",Sum(d)-Sum(i-1));                else if(a[0]=='A') Add(i,d);                else if(a[0]=='S') Add(i,-d);            }        }    return 0;}



热点排行