HDU 1166 敌兵布阵(树状数组)

题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25681#problem/A

代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int a[50005],n;
int lowbit(int t)
{
    return t&(-t);
}
int insert(int t,int d)
{
    while(t<=n)
    {
        a[t]=a[t]+d;
        t=t+lowbit(t);
    }
}
long long getsum(int t)
{
    long long ans=0;
    while(t>0)
    {
        ans=ans+a[t];
        t=t-lowbit(t);
    }
    return ans;
}
int main()
{
    int t,temp=0;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int k;
            scanf("%d",&k);
            insert(i,k);
        }
        char str[10];
        printf("Case %d:\n",++temp);
        while(scanf("%s",str)&&(strcmp(str,"End")!=0))
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(strcmp(str,"Query")==0)
            {
                printf("%d\n",getsum(y)-getsum(x-1));
            }
            else if(strcmp(str,"Add")==0)
            {
                insert(x,y);
            }
            else if(strcmp(str,"Sub")==0)
            {
                insert(x,(-1)*y);
            }
        }
    }
}


阅读更多
个人分类: HDU
上一篇codeforces 676C Vasya and String (尺取法)
下一篇POJ 3264 Balanced Lineup(RMQ)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭