线段树代码30分求助
查看原帖
线段树代码30分求助
363056
川建国已暴露楼主2021/8/3 09:43
#include<iostream>
#include<cstdio>
#define M 1000001
#define ll long long
#define lson (o<<1)
#define rson (o<<1|1)
using namespace std;
ll n,m,t,l,r,k,p,a1[M],sumv[M<<2],minv[M<<2],tag[M<<2],tag2[M<<2];
inline int read(){
    ll x=0,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-48;ch=getchar();}
    return x*f;
}
inline void pushup(ll o){sumv[o]=sumv[lson]+sumv[rson];}
inline void pushdown(ll o,ll l,ll r){
    ll mid=(l+r)>>1;
    sumv[lson]=(tag2[o]*sumv[lson]+tag[o]*(mid-l+1))%p;
    sumv[rson]=(tag2[o]*sumv[rson]+tag[o]*(r-mid))%p;
    tag2[lson]=(tag2[o]*tag2[lson])%p;
    tag2[rson]=(tag2[o]*tag2[rson])%p;
    tag[lson]=(tag[o]+tag[lson])%p;
    tag[rson]=(tag[o]+tag[rson])%p;
    tag[o]=0;
    tag2[o]=1;
}
inline void build(ll o,ll l,ll r){
	tag[o]=0;
	tag2[o]=1;
    if(l==r){sumv[o]=a1[l];return;}
    ll mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    pushup(o);
}
inline void change1(ll o,ll l,ll r,ll k,ll ql,ll qr){
	if(ql<=l&&r<=qr){
		sumv[o]=(k*sumv[o])%p;
		tag[o]=(k*tag[o])%p;
		tag2[o]=(k*tag2[o])%p;
		return;
	}
	ll mid=(l+r)/2;
	pushdown(o,l,r);
	if(ql<=mid)change1(lson,l,mid,k,ql,qr);
	if(qr>mid)change1(rson,mid+1,r,k,ql,qr);
	pushup(o);
}
inline void change2(ll o,ll l,ll r,ll k,ll ql,ll qr){
    if(ql<=l&&r<=qr){
		sumv[o]=(k*(r-l+1))%p+sumv[o]%p;
		tag[o]=(k+tag[o])%p;
		return;
	}
    ll mid=(l+r)/2;
    pushdown(o,l,r);
    if(ql<=mid)change2(lson,l,mid,k,ql,qr);
    if(qr>mid)change2(rson,mid+1,r,k,ql,qr);
    pushup(o);
}
inline ll querysum(ll o,ll l,ll r,ll ql,ll qr){
	ll res=0;
    if(ql<=l&&r<=qr)return sumv[o];
    ll mid=(l+r)>>1;
    pushdown(o,l,r);
    if(ql<=mid)res=(res+querysum(lson,l,mid,ql,qr))%p;
    if(qr>mid)res=(res+querysum(rson,mid+1,r,ql,qr))%p;
    return res%p;
}
int main(){
    n=read(),m=read(),p=read();
    for(int i=1;i<=n;i++)a1[i]=read();
    build(1,1,n);
    while(m--){
        t=read();
        if(t==1){
            l=read(),r=read(),k=read();
            change1(1,1,n,k,l,r);
        }
        if(t==2){
            l=read(),r=read(),k=read();
            change2(1,1,n,k,l,r);
        }
        if(t==3){
        	l=read(),r=read();
        	printf("%lld\n",querysum(1,1,n,l,r));
        }
    }
} 
2021/8/3 09:43
加载中...