HDU 1166 敌兵布阵

树状数组的模版题,还是一维的!直接A掉就行!

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
int
num=0;
int
tree[50005],tmp[50005];
inline
int lowbit(int t)
{

    return
t & (t ^ (t-1));
}


inline
void update(int pot,int val)
{

    while
(pot <=num)
    {


     tree[pot]+=val;
     //if (pot==1) {cout<<tree[1]<<" "<<val<<endl;}
     pot+=lowbit(pot);

    }



}


inline
int query(int pot)
{

    int
sum=0;
    while
(pot>0)
    {

      sum+=tree[pot];
      pot-= lowbit(pot);
    }


   return
sum;
}





int
main()
{

     int
t,n,a,b;
     char
s[10];
     cin>>t;
      for
(int k=1;k<=t;k++)
     {

      memset(tree,0,sizeof(tree));
      cout<<"Case "<<k<<":"<<endl;
      scanf("%d",&n);
      num=n;
      for
(int i=1;i<=n;i++)
      {

          scanf("%d",&tmp[i]);
          update(i,tmp[i]);
      }

      while
(scanf("%s",s))
     {

       //scanf("%s",s);
       if (strcmp(s,"End")==0) break;
       if
(strcmp(s,"Add")==0)
       {

           scanf("%d%d",&a,&b);
           update(a,b);

       }

       if
(strcmp(s,"Sub")==0)
       {

           scanf("%d%d",&a,&b);
           update(a,-1*b);

       }


       if
(strcmp(s,"Query")==0)
       {

           scanf("%d%d",&a,&b);

           int
ans=query(b)-query(a-1);
           printf("%d\n",ans);
       }


     }

    //for(int i=1;i<=4;i++)
    //cout<<tree[i]<<endl;
    //cout<<query(4)<<endl;

}

    //cout << "Hello world!" << endl;
    return 0;
}

 

posted @ 2012-09-21 19:10  兴安黑熊  阅读(162)  评论(0编辑  收藏  举报