LightOJ1112——Curious Robin Hood

线段树区间总和,单点更新。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

const int maxN=100010;
int tree[4*maxN];
int money[maxN];

void build(int no,int l,int r)
{
    if(l==r)
    {
        tree[no]=money[l];
        return;
    }

    int mid=(l+r)>>1;
    int no2=no<<1;
    build(no2,l,mid);
    build(no2+1,mid+1,r);
    tree[no]=tree[no2]+tree[no2+1];
}

void update1(int no,int l,int r,int x)
{
    if(l==r)
    {
        printf("%d\n",tree[no]);
        tree[no]=0;
        return;
    }

    int mid=(l+r)>>1;
    int no2=no<<1;
    if(x<=mid) update1(no2,l,mid,x);
    else update1(no2+1,mid+1,r,x);
    tree[no]=tree[no2]+tree[no2+1];
}

void update2(int no,int l,int r,int x,int change)
{
    if(l==r)
    {
        tree[no]+=change;
        return;
    }

    int mid=(l+r)>>1;
    int no2=no<<1;
    if(x<=mid) update2(no2,l,mid,x,change);
    else update2(no2+1,mid+1,r,x,change);
    tree[no]=tree[no2]+tree[no2+1];
}

int query(int no,int l,int r,int a,int b)
{
    if(a==l && b==r)
    {
        return tree[no];
    }

    int mid=(l+r)>>1;
    int no2=no<<1;
    if(b<=mid) return query(no2,l,mid,a,b);
    if(a>mid) return query(no2+1,mid+1,r,a,b);
    return query(no2,l,mid,a,mid)+query(no2+1,mid+1,r,mid+1,b);
}

int main()
{
    int t;

    //freopen("stdin.txt","r",stdin);
    scanf("%d",&t);
    for(int id=1; id<=t; id++)
    {
        int n,m;
        int cas;
        int x,y;

        printf("Case %d:\n",id);
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%d",&money[i]);
        build(1,1,n);
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&cas);
            switch(cas)
            {
            case 1:
                scanf("%d",&x);
                x+=1;
                update1(1,1,n,x);
                break;
            case 2:
                scanf("%d %d",&x,&y);
                x+=1;
                update2(1,1,n,x,y);
                break;
            case 3:
                scanf("%d %d",&x,&y);
                x+=1,y+=1;
                printf("%d\n",query(1,1,n,x,y));
                break;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值