样例不过求调

P3373 【模板】线段树 2

@[ISU152_YYDS](/user/933802) 和我的对一下吧,你的pushdown写的着实抽象( ```cpp #include <iostream> using namespace std; #define int long long int m,n,q,a[400010],t1[400010],t2[400010],yuan[400010],qwq[400010],awa[400010]; void apply(int p,int l,int r,int k1,int k2){ a[p]*=k1; a[p]%=m; a[p]+=(r-l+1)*k2; a[p]%=m; t1[p]*=k1; t1[p]%=m; t2[p]*=k1; t2[p]%=m; t2[p]+=k2; t2[p]%=m; return ; } void pushdown(int p,int l,int r){ apply(2*p,l,(l+r)/2,t1[p],t2[p]); apply(2*p+1,(l+r)/2+1,r,t1[p],t2[p]); t1[p]=1; t2[p]=0; return ; } void pushup(int p){ a[p]=(a[p*2]+a[p*2+1])%m; return ; } void build(int l,int r,int p){ qwq[p]=l; awa[p]=r; if(l==r){ a[p]=yuan[l]%m; return ; } build(l,(l+r)/2,p*2); build((l+r)/2+1,r,p*2+1); pushup(p); } void jia(int p,int l,int r,int ql,int qr,int k){ //cout<<"左边界="<<l<<" 右边界="<<r<<endl; if(l>qr||r<ql) return; if(ql<=l&&qr>=r){ //cout<<1<<endl; apply(p,l,r,1,k); return ; } pushdown(p,l,r); jia(2*p,l,(l+r)/2,ql,qr,k); jia(2*p+1,(l+r)/2+1,r,ql,qr,k); pushup(p); return ; } void cheng(int p,int l,int r,int ql,int qr,int k){ //cout<<"左边界="<<l<<" 右边界="<<r<<endl; if(l>qr||r<ql) return; if(ql<=l&&qr>=r){ apply(p,l,r,k,0); return ; } pushdown(p,l,r); cheng(2*p,l,(l+r)/2,ql,qr,k); cheng(2*p+1,(l+r)/2+1,r,ql,qr,k); pushup(p); return ; } int an(int p,int l,int r,int ql,int qr){ if(l>qr||r<ql) return 0; if(l>=ql&&r<=qr){ //cout<<"节点编号为"<<p<<" 左边界为"<<l<<" 右边界为"<<r<<" 值为"<<a[p]<<endl; return a[p]; } pushdown(p,l,r); int tmp1=(an(2*p,l,(l+r)/2,ql,qr))%m,tmp2=(an(2*p+1,(l+r)/2+1,r,ql,qr))%m; //cout<<"节点编号为"<<p<<" 左边界为"<<l<<" 右边界为"<<r<<" 左子树值为"<<tmp1<<" 右子树值为"<<tmp2<<endl; return (tmp1+tmp2)%m; } signed main(){ cin>>n>>q>>m; for(int i=1;i<=n;i++) cin>>yuan[i]; for(int i=0;i<=400005;i++) t1[i]=1; build(1,n,1); while(q--){ int op,x,y,zhi; cin>>op; if(op==2){ cin>>x>>y>>zhi; jia(1,1,n,x,y,zhi); } else if(op==1){ cin>>x>>y>>zhi; cheng(1,1,n,x,y,zhi); } else{ cin>>x>>y; cout<<an(1,1,n,x,y)<<endl; } /*cout<<"!!!!!!"; for(int i=1;i<=20;i++) cout<<"左边界="<<qwq[i]<<" 右边界="<<awa[i]<<" 和为"<<a[i]<<" t1[i]="<<t1[i]<<" t2[i]=="<<t2[i]<<endl; cout<<endl;*/ } return 0; } /* ┏┓   ┏┓ ┏┛┻━━━┛┻┓ ┃       ┃ ┃   ━   ┃ ┃ ┳┛ ┗┳ ┃ ┃       ┃ ┃   ┻   ┃ ┃       ┃ ┗━┓   ┏━┛Codes are far away from bugs with the animal protecting ┃   ┃ 神兽保佑,代码无bug ┃   ┃ ┃   ┗━━━┓ ┃      ┣┓ ┃     ┏┛ ┗┓┓┏━┳┓┏┛ ┃┫┫ ┃┫┫ ┗┻┛ ┗┻┛ ○| ̄|_ */ ```
by rnfmabj5114 @ 2023-12-29 21:16:42


@[zyh0516_lucky](/user/746930) 请问区别在什么位置,谢谢
by ICU152_QWQ_IS8 @ 2023-12-29 21:38:48


@[ISU152_YYDS](/user/933802) 你update1里面%成k了
by Paradise_Lost @ 2023-12-29 21:41:01


@[ISU152_YYDS](/user/933802) 我这么长代码就是改了update1中的%k为%m
by zyh0516_lucky @ 2023-12-29 21:43:07


@[rnfmabj5114](/user/917683) 楼主想要参考代码可以看题解。而且只是写法不同而已,楼主代码哪里抽象了?
by zyh0516_lucky @ 2023-12-29 21:44:21


@[zyh0516_lucky](/user/746930) @[Paradise_Lost](/user/688649) 谢谢,关了
by ICU152_QWQ_IS8 @ 2023-12-29 21:45:55


@[zyh0516_lucky](/user/746930) 我感觉代码太长了蛮抽象的,写个函数不就缩了好多嘛,还好找错
by rnfmabj5114 @ 2023-12-29 21:46:30


|