社区讨论
WA求调
P3373【模板】线段树 2参与者 2已保存回复 8
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 8 条
- 当前快照
- 1 份
- 快照标识符
- @mlirw60i
- 此快照首次捕获于
- 2026/02/12 09:21 上周
- 此快照最后确认于
- 2026/02/14 13:35 5 天前
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,q,m;
struct note{
int l,r,sum,addj,addc;
} tree[N<<2];
int a[N];
void build(int x,int l,int r){
tree[x].l=l;
tree[x].r=r;
tree[x].addc=1;
if(l==r){
tree[x].sum=a[l]%m;
return ;
}
int mid=(l+r)/2;
build(x*2,l,mid);
build(x*2+1,mid+1,r);
tree[x].sum=(tree[x*2].sum+tree[x*2+1].sum)%m;
}
void lazy(int x){
int tag=tree[x].addj;
int tag2=tree[x].addc;
if(tag2!=1){
tree[x<<1].sum=tree[x<<1].sum%m*tag2%m;
tree[x<<1].addc=tree[x<<1].addc%m*tag2%m;
tree[x<<1].addj=(tree[x<<1].addj%m*tag2+tag)%m;
tree[x<<1|1].sum=tree[x<<1|1].sum%m*tag2%m;
tree[x<<1|1].addc=tree[x<<1|1].addc%m*tag2%m;
tree[x<<1|1].addj=(tree[x<<1|1].addj%m*tag2+tag)%m;
tree[x].addc=1;
}
if(tag){
tree[x<<1].sum=(tree[x<<1].sum+(tree[x<<1].r-tree[x<<1].l+1)*tag)%m;
tree[x<<1].addj=(tree[x<<1].addj+tag)%m;
tree[x<<1|1].sum=(tree[x<<1|1].sum+(tree[x<<1|1].r-tree[x<<1|1].l+1)*tag)%m;
tree[x<<1|1].addj=(tree[x<<1|1].addj+tag)%m;
tree[x].addj=0;
}
}
void cheng(int x,int l,int r,int k){
if(tree[x].l>=l&&tree[x].r<=r){
tree[x].sum=tree[x].sum*k%m;
tree[x].addc=tree[x].addc*k%m;
tree[x].addj=tree[x].addj*k%m;
return ;
}
lazy(x);
int mid=(tree[x].l+tree[x].r)>>1;
if(mid>=l) cheng(x*2,l,mid,k);
if(mid<r) cheng(x*2+1,mid+1,r,k);
tree[x].sum=(tree[x<<1].sum+tree[x<<1|1].sum)%m;
}
void jia(int x,int l,int r,int k){
if(tree[x].l>=l&&tree[x].r<=r){
tree[x].sum=(tree[x].sum+(tree[x].r-tree[x].l+1)*k)%m;
tree[x].addj=(tree[x].addj+k)%m;
return ;
}
lazy(x);
int mid=(tree[x].l+tree[x].r)>>1;
if(mid>=l) jia(x*2,l,mid,k);
if(mid<r) jia(x*2+1,mid+1,r,k);
tree[x].sum=(tree[x<<1].sum+tree[x<<1|1].sum)%m;
}
int query(int x,int l,int r){
if(tree[x].l>=l&&tree[x].r<=r) return tree[x].sum%m;
lazy(x);
int mid=(tree[x].l+tree[x].r)>>1;
int cnt=0;
if(mid>=l) cnt=(cnt+query(x<<1,l,r))%m;
if(mid<r) cnt=(cnt+query(x<<1|1,l,r))%m;
return cnt%m;
}
signed main(){
cin>>n>>q>>m;
for(int i=1;i<=n;i++) cin>>a[i],a[i]%=m;
build(1,1,n);
for(int i=1;i<=q;i++){
int t,x,y,k;
cin>>t;
if(t==1){
cin>>x>>y>>k;
cheng(1,x,y,k%m);
}else if(t==2){
cin>>x>>y>>k;
jia(1,x,y,k%m);
}else{
cin>>x>>y;
cout<<query(1,x,y)%m<<"\n";
}
}
return 0;
}
样例过了,爆0
回复
共 8 条回复,欢迎继续交流。
正在加载回复...