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

垃圾HDU,不给数据,浪费我大好青春!


 

【题目信息】

敌兵布阵

 

【分析】

树状数组的裸题,单点修改+区间查询。

 

【心路历程】

脑残的我固输“Case 1”,memset了个sizeof(0)......

……

 

【代码】

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,a;
int c[110005];
int lowbit(int x)
{ 
    return x&(-x);
}

void update(int x,int y,int n)
{
    while(x<=n)
    {
        c[x]+=y;
        x+=lowbit(x);
    }
}

int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}

int main()
{
    int t,x,y,k=1;
    char s[1005];
    scanf("%d",&t);
    while(t--)
    {
        memset(c,0,sizeof(c));//de了一下午,才发现自己写的是sizeof(0) 
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            update(i,a,n);
        }
            printf("Case %d:\n",k++);
         while(~scanf("%s",s))
        {
            if(s[0]=='E') break;
            scanf("%d%d",&x,&y); 
            if(s[0]=='Q')
            {
                printf("%d\n",sum(y)-sum(x-1));
            }
            else if(s[0]=='A')
            {
                update(x,y,n);
            }
            else if(s[0]=='S')
            {
                update(x,-y,n);
            }
        }
    }
    return 0;
}

 

posted @ 2021-08-27 11:06  TheZealous  阅读(21)  评论(0编辑  收藏  举报