hdu-1166-敌兵布阵-线段树-单点更新,区域查询

线段树的单点更新,区域查询操作。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define maxn 55000
int cl[maxn*4];
int num[maxn];
void push_up(int rt)
{
    cl[rt]=cl[rt<<1]+cl[rt<<1|1];
}
void push_down(int rt)
{
    if(cl[rt]!=-1)
    {
        cl[rt<<1]=cl[rt];
        cl[rt<<1|1]=cl[rt];
        cl[rt]=-1;
    }
}
void creat(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&cl[rt]);
        return;
    }
    creat(lson);
    creat(rson);
    push_up(rt);
}
void update(int ll,int rr,int x,int l,int r,int rt)
{
    if(l>rr||r<ll)return;
    if(ll<=l&&rr>=r)
    {
        cl[rt]+=x;
        return;
    }
   // push_down(rt);
    update(ll,rr,x,lson);
    update(ll,rr,x,rson);
    push_up(rt);
}
int query(int ll,int rr,int l,int r,int rt)
{
    if(r<ll)return 0;
    if(l>rr)return 0;
    if(ll<=l&&rr>=r)return cl[rt];
    return query(ll,rr,lson)+query(ll,rr,rson);
}
int main()
{
    int T,_,n,l,r,st,x;
    char str[110];
    scanf("%d",&T);
    for(_=1;_<=T;_++)
    {
        printf("Case %d:\n",_);
        scanf("%d",&n);
        creat(root);
        while(1)
        {
            scanf("%s",str);
            if(str[0]=='E')break;
            else if(str[0]=='Q')
            {
                scanf("%d%d",&l,&r);
                printf("%d\n",query(l,r,root));
            }
            else
            {
                scanf("%d%d",&st,&x);
                if(str[0]=='A')update(st,st,x,root);
                else update(st,st,-x,root);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值