您的位置:首页 > 其它

hdu 1166 敌兵布阵

2014-01-03 19:38 393 查看
线段树单点更新。

单点增减 区间求和。

(刚刚开始学线段树T。T....)

#include<cstdio>
#include<iostream>
#define lson l , m , rt<<1
#define rson m+1 , r , rt<<1|1
const int maxn = 55555;

int sum[maxn<<2];

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);
}

void Update(int p,int add ,int l,int r ,int rt )
{
    if(l == r)
    {
        sum[rt]+=add;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m) Update(p,add,lson);
    else Update(p,add,rson);
    PushUP(rt);
}

int Queue(int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)
        return sum[rt];

    int res=0;
    int m=(l+r)>>1;
    if(L <= m) res += Queue(L, R, lson);
    if(R > m) res += Queue(L, R, rson);
    return res;
}

int main()
{
    int T,n;
    scanf("%d",&T);
    for(int cas = 1;cas <= T; cas++)
    {
        printf("Case %d:\n",cas);
        scanf("%d",&n);
        build(1,n,1);
        char ask[20];
        while(scanf("%s",ask))
        {
            if(ask[0]=='E')
                break;
            int a,b;
            scanf("%d%d",&a,&b);
            if(ask[0]=='Q')
                printf("%d\n",Queue(a,b,1,n,1));
            else if(ask[0]=='A')
                Update(a,b,1,n,1);
            else
                Update(a,-b,1,n,1);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: