hdu1166

简单的线段树题目:单点更新。
#include<stdio.h>
#include<string.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max 50010
int sum[max<<2];
int map[max];
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;
    int ret=0;
    if(L<=m)
        ret+=query(L,R,lson);
    if(R>m)
        ret+=query(L,R,rson);
    return ret;
}
void update(int a,int add,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]+=add;
        return ;
    }
    int m=(l+r)>>1;
    if(a<=m)
        update(a,add,lson);
    else
        update(a,add,rson);
    PushUp(rt);//往上更新。
}
int main()
{
    int t;
    int a,b;
    int n;
    char op[12];
    scanf("%d",&t);
    for(int i=1; i<=t; i++)
    {
        printf("Case %d:\n",i);
        scanf("%d",&n);
        build(1,n,1);
        while(1)
        {
            scanf("%s",op);
            if(op[0]=='E')
                break;
            scanf("%d%d",&a,&b);
            if(op[0]=='Q')
            {
                printf("%d\n",query(a,b,1,n,1));
            }
            else if(op[0]=='S')
            {
                update(a,-b,1,n,1);
            }
            else if(op[0]=='A')
            {
                update(a,b,1,n,1);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(hdu1166)