XDOJ1279

题目

记录修改 线段树求和

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
inline int read()
{
    int x=0;char ch=0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x;
}

const int maxn = 1e5+100;
int arr[maxn],tree[maxn*20],gs[maxn*20];
void add(int l,int r,int rt,int pos)
{
    if(arr[rt]!=91)gs[pos]++;
    tree[pos]+=arr[rt];
    if(l==r){      
        return;
    }
    int m=l+r>>1;
    if(m>=rt)
        add(l,m,rt,pos<<1);
    else 
        add(m+1,r,rt,pos<<1|1);
}

void change(int l,int r,int L,int R,int pos)
{
	//cout<<l<<" "<<r<<" "<<gs[pos]<<" "<<tree[pos]<<endl;
    if(gs[pos]==0)return;
    if(l==r&&tree[pos]>101)
    {
        tree[pos]-=10;
        return;
    }
    else if(l==r&&tree[pos]<=101)
    {
        tree[pos]=91;
        if(tree[pos]==91) gs[pos]=0;
        return;
    }
    if(l==r&&tree[pos]==91)return;
    int m=l+r>>1;

	if(m>=L)change(l,m,L,R,pos<<1);
    if(m<R)change(m+1,r,L,R,pos<<1|1);
	
    gs[pos]=gs[pos<<1]+gs[pos<<1|1];
    tree[pos]=tree[pos<<1]+tree[pos<<1|1];
}

int qry(int l,int r,int L,int R,int pos)
{
    if(l>=L &&r<=R) return tree[pos];
    int m=l+r>>1;
    int sum=0;
    if(m>=L)sum+=qry(l,m,L,R,pos<<1);
    if(m<R)sum+=qry(m+1,r,L,R,pos<<1|1);
    return sum;
}

int main()
{
	//freopen("date.in","r",stdin);
    int n=read();
    for(int i=1;i<=n;i++)
    {
        arr[i]=read();
        add(1,n,i,1);
    }
    int d = read();
    for(int i=1;i<=d;i++)
    {
        int a=read(),b=read(),c=read();
        if(a==1)
            change(1,n,b,c,1);
        else 
            printf("%d\n",qry(1,n,b,c,1));
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值