HDU 1166 树状数组 基础题

/*中文题,很好理解。 http://acm.hdu.edu.cn/showproblem.php?pid=1166贴个链接

比较纠结的是TLE了好几次。。原因居然是。。。。。*/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 50005
#define inf 1<<28
using namespace std;

int peonum,c[Max],m;
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x,int y)
{
    while(x<=m)
    {
        c[x]+=y;
        x+=lowbit(x);
    }
}
int query(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int i,j,k,l,n;
    char aa[10];
    int x1,y1,s;
    int kk=0;
    scanf("%d",&n);

    while(n--)
    {
        printf("Case %d:\n",++kk);
        scanf("%d",&m);
        memset(c,0,sizeof(c));
        for(i=1; i<=m; i++)
            scanf("%d",&peonum),updata(i,peonum);
        scanf("%s",aa);
        while(aa[0]!='E')
        {
            scanf("%d%d",&x1,&y1);
            if(aa[0]=='Q')
            {
                printf("%d\n",query(y1)-query(x1-1));//一开始没注意啊。直接写cout....然后各种TLE死都找不出哪里有问题。。随手换了个printf..就A了。。。
            }
            else if(aa[0]=='A')
            {
                updata(x1,y1);
            }
            else
            {
                updata(x1,-y1);
            }
            scanf("%s",aa);
        }
    }
    return 0;
}

还是有点粗心啊。。继续加油~

你可能感兴趣的:(HDU 1166 树状数组 基础题)