Segment tree simple title set

 

Segment tree [] codeforces - 438D and Sequence of The Child (section modulus)

Title effect:
There are three operations: 1 segment interval summation modulo 2. 3. A single point updating

Problem solving analysis:
The main consideration is how to deal with the interval modulo operation, can not always have all the modulo the number of violent modulo time, there is a very common pruning, maintenance interval maximum value, only when the maximum value is greater than the mold number when we come down to continue modulo operation, otherwise terminate. Because there is a very obvious conclusion is possible for each modulo number, after the modulo, it inevitably becomes less than or equal to its number {2} $ FRAC of $ \ {1}. So each log number will be up to $ $ molded times, the interval modulo complexity is $ O (nlogn) $.

#include <bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T&x){        
    x=0;int f=1;char ch=getchar();
    while(ch<'0' ||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); }
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
    x*=f;
}
#define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
#define ll long long 
#define REP(i,s,t) for(int i=s;i<=t;i++)

const int N = 1e5+5;
ll mx[N<<2],sum[N<<2],a[N];
int n,m;

inline void Pushup(int rt){
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int rt,int l,int r){
    if(l==r){
        mx[rt]=sum[rt]=a[l];
        return;
    }
    int mid=l+r>>1;
    build(Lson);build(Rson);
    Pushup(rt);
}
void up1(int rt,int l,int r,int loc,int val){              //单点更新
    if(l==r){ mx[rt]=sum[rt]=val; return; }
    int mid=(l+r)>>1;
    if(loc<=mid)up1(Lson,loc,val);
    else up1(Rson,loc,val);
    Pushup(rt);
}
void up2(int rt,int l,int r,int L,int R,int md){
    if(md>mx[rt])return;
    if(l==r){
        sum[rt]%=md;mx[rt]=sum[rt];
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid)up2(Lson,L,R,md);
    if(R>mid)up2(Rson,L,R,md);
    Pushup(rt);
}
ll query(int rt,int l,int r,int L,int R){
    if(L<=l&&r<=R)return sum[rt];
    int mid=(l+r)>>1;
    ll ans=0;
    if(L<=mid)ans+=query(Lson,L,R);
    if(R>mid)ans+=query(Rson,L,R);
    return ans;
}
int main(){
    read(n);read(m);
    REP(i,1,n)read(a[i]);
    build(1,1,n);
    while(m--){
        int op;read(op);
        if(op==1){
            int l,r;read(l);read(r);
            printf("%lld\n",query(1,1,n,l,r));
        }else if(op==2){
            int l,r,md;read(l);read(r);read(md);
            up2(1,1,n,l,r,md);
        }else{
            int loc,c;read(loc);read(c);
            up1(1,1,n,loc,c);
        }
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/00isok/p/10994865.html