[均摊复杂度线段树] Codeforces #438D. The Child and Sequence

水~~~

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200005;
typedef unsigned long long  uLL; 
struct node{
    node* ch[2];
    uLL sum,_max;   
    node(uLL t1=0,uLL t2=0,node* t3=0){ sum=t1; _max=t2; ch[0]=ch[1]=t3; }
    void maintain(){
        _max=max(ch[0]->_max,ch[1]->_max);
        sum=ch[0]->sum+ch[1]->sum;
    }
} *root;
typedef node* P_node;
P_node Build(int L,int R){
    P_node p=new node();
    if(L==R){
        scanf("%d",&p->sum); p->_max=p->sum;
        return p;
    }
    int mid=(L+R)>>1;
    p->ch[0]=Build(L,mid); p->ch[1]=Build(mid+1,R);
    p->maintain(); return p;
}
void Updata(P_node p,int L,int R,int pos,int val){
    if(pos<L||R<pos) return;
    if(L==R){ p->sum=p->_max=val; return; }
    int mid=(L+R)>>1;
    Updata(p->ch[0],L,mid,pos,val); Updata(p->ch[1],mid+1,R,pos,val);
    p->maintain();
}
void Updata(P_node p,int L,int R,int qL,int qR,int Mod){
    if(qR<L||R<qL||p->_max<Mod) return;
    if(L==R){ p->_max=(p->sum%=Mod); return; }
    int mid=(L+R)>>1;
    Updata(p->ch[0],L,mid,qL,qR,Mod); Updata(p->ch[1],mid+1,R,qL,qR,Mod);
    p->maintain();
}
uLL Query(P_node p,int L,int R,int qL,int qR){
    if(qR<L||R<qL) return 0;
    if(qL<=L&&R<=qR) return p->sum;
    int mid=(L+R)>>1;
    return Query(p->ch[0],L,mid,qL,qR)+Query(p->ch[1],mid+1,R,qL,qR);
}
int n,Q;
int main(){
    freopen("cf438D.in","r",stdin);
    freopen("cf438D.out","w",stdout);
    scanf("%d%d",&n,&Q);
    root=Build(1,n);
    while(Q--){
        int _type,x,y,z; scanf("%d",&_type);
        if(_type==1){
            scanf("%d%d",&x,&y); printf("%I64d\n",Query(root,1,n,x,y));
        } else
        if(_type==2){
            scanf("%d%d%d",&x,&y,&z); Updata(root,1,n,x,y,z);
        } else
        if(_type==3){
            scanf("%d%d",&x,&y); Updata(root,1,n,x,y);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值