HDOJ1166 敌兵布阵【线段树】

Problem : 1166 ( 敌兵布阵 )     Judge Status : Accepted
RunId : 5862942    Language : GCC    Author : qq1203456195

#include <stdio.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define 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)
{
    int m;
    if (l==r)
    {
        scanf("%d",&sum[rt]);
        return;
    }
    m=((l+r)>>1);
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int p,int add,int l,int r,int rt)
{
    int m;
    if (l==r)
    {
        sum[rt]+=add;
        return;
    }
    m=((l+r)>>1);
    if (p<=m)
        update(p,add,lson);
    else
        update(p,add,rson);
    PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    int m,ret=0;
    if (L<=l&&R>=r)
    {
        return sum[rt];
    }
    m=((l+r)>>1);
    if (L<=m)
        ret+=query(L,R,lson);
    if (R>m)
        ret+=query(L,R,rson);
    return ret;
}
int main()
{
    int T,n,cas;
    char op[10];
    int a,b;
    scanf("%d",&T);

    for (cas=1;cas<=T;cas++)
    {
        printf("Case %d:\n",cas);
        scanf("%d",&n);
        build(1,n,1);
        while (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
                    update(a,b,1,n,1);
        }
    }

    return 0;
}
字节跳动内推

找我内推: 字节跳动各种岗位
作者: ZH奶酪(张贺)
邮箱: cheesezh@qq.com
出处: http://www.cnblogs.com/CheeseZH/
* 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/CheeseZH/p/2475493.html