样例不过,求找茬

P3373 【模板】线段树 2

@[mc360](/user/768587) 你的操作3去哪里了?
by Big_Dinosaur @ 2024-01-04 14:51:09


@[mc360](/user/768587) 你的操作3呢大哥
by liverxiwo @ 2024-01-04 15:15:11


@[mc360](/user/768587) 看我代码自己改去 ```cpp #include<bits/stdc++.h> #define ll long long using namespace std; inline int read1() { int 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 ll read2(){ register ll x=0,f=1;register char c=getchar(); for(;!isdigit(c);c=getchar())if(c=='-')f=-1; for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); return x*f; } inline void put(int x) { if(x==0) { putchar('0'); putchar('\n'); return; } int num=0; char c[25]; while(x) { c[++num]=(x%10)+48; x/=10; } while(num) putchar(c[num--]); putchar('\n'); return ; } int p; long long sum[100001]; struct node{ long long ans,mul,add; }st[400001]; void build(int root,int l,int r){ st[root].mul=1; st[root].add=0; if(l==r){ st[root].ans=sum[l]; } else{ int mid=(l+r)/2; build(root*2,l,mid); build(root*2+1,mid+1,r); st[root].ans=st[root*2].ans+st[root*2+1].ans; } st[root].ans%=p; return; } void pushdown(int root, int l, int r){ int mid=(l+r)/2; st[root*2].ans=(st[root*2].ans*st[root].mul+st[root].add*(mid-l+1))%p; st[root*2+1].ans=(st[root*2+1].ans*st[root].mul+st[root].add*(r-mid))%p; st[root*2].mul=(st[root*2].mul*st[root].mul)%p; st[root*2+1].mul=(st[root*2+1].mul*st[root].mul)%p; st[root*2].add=(st[root*2].add*st[root].mul+st[root].add)%p; st[root*2+1].add=(st[root*2+1].add*st[root].mul+st[root].add)%p; st[root].mul=1; st[root].add=0; return ; } void multiplication(int root,int stdl,int stdr,int l,int r,long long k){ int mid=(stdl+stdr)/2; if(r<stdl || stdr<l){ return ; } if(l<=stdl && stdr<=r){ st[root].ans=(st[root].ans*k)%p; st[root].mul=(st[root].mul*k)%p; st[root].add=(st[root].add*k)%p; return ; } pushdown(root, stdl, stdr); multiplication(root*2, stdl, mid, l, r, k); multiplication(root*2+1, mid+1, stdr, l, r, k); st[root].ans=(st[root*2].ans+st[root*2+1].ans)%p; return ; } void addition(int root,int stdl,int stdr,int l,int r,long long k){ if(r<stdl || stdr<l){ return ; } if(l<=stdl && stdr<=r){ st[root].add=(st[root].add+k)%p; st[root].ans=(st[root].ans+k*(stdr-stdl+1))%p; return ; } pushdown(root, stdl, stdr); int mid=(stdl+stdr)/2; addition(root*2, stdl, mid, l, r, k); addition(root*2+1, mid+1, stdr, l, r, k); st[root].ans=(st[root*2].ans+st[root*2+1].ans)%p; return; } long long query(int root,int stdl,int stdr,int l,int r){ int mid=(stdl+stdr)/2; if(r<stdl || stdr<l){ return 0; } if(l<=stdl && stdr<=r){ return st[root].ans; } pushdown(root, stdl, stdr); ll ans=(query(root*2, stdl, mid, l, r)+query(root*2+1, mid+1, stdr, l, r))%p; return ans; } int n,m,opt,x,y; int main(){ n=read1(),m=read1(),p=read1(); for(int i=1;i<=n;i++){ sum[i]=read2(); } build(1,1,n); for(int i=1;i<=m;i++){ opt=read1(); ll k; if(opt==1){ x=read1(),y=read1(),k=read2(); multiplication(1,1,n,x,y,k); } else { if(opt==2){ x=read1(),y=read1(),k=read2(); addition(1,1,n,x,y,k); } else{ x=read1(),y=read1(); int ans=query(1,1,n,x,y); put(ans); } } } return 0; } ```
by liverxiwo @ 2024-01-04 15:18:01


@[mc360](/user/768587) 下次找人帮忙记得加注释。。。
by liverxiwo @ 2024-01-04 15:18:48


|