社区讨论
何意味
学术版参与者 7已保存回复 13
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 13 条
- 当前快照
- 1 份
- 快照标识符
- @mi7es9ai
- 此快照首次捕获于
- 2025/11/20 20:29 3 个月前
- 此快照最后确认于
- 2025/11/21 00:19 3 个月前
线段树2模板的代码,在洛谷评测机和学校电脑的devc++上能正确运行,但在我的vscode里会出现segmentation fault,vscode的环境是没有问题的,其他程序都能正常运行
CPP//P3373 【模板】线段树 2
#include<iostream>
#include<iomanip>
using namespace std;
long long w[400005],a[100001],lzya[400005],lzym[400005];
int p;
void pushup(int u)
{
w[u]=(w[u*2]+w[u*2+1])%p;
}
void build(int u,int l,int r)
{
if(l==r)
{
w[u]=a[l];
return;
}
int m=(l+r)>>1;
lzym[u]=1;
build(u*2,l,m);
build(u*2+1,m+1,r);
pushup(u);
}
bool inRange(int l,int r,int L,int R)
{
return (l<=L)&&(R<=r);
}
bool outOfRange(int l,int r,int L,int R)
{
return (L>r)||(R<l);
}
void maketag(int u,int L,int R,long long x,int type)
{
if(type=='M')
{
(lzya[u]*=x)%=p;
(lzym[u]*=x)%=p;
(w[u]*=x)%=p;
}
else
{
(lzya[u]+=x)%=p;
(w[u]+=(R-L+1)*x)%=p;
}
}
void pushdown(int u,int L,int R)
{
int m=(L+R)>>1;
maketag(u*2,L,m,lzym[u],'M');
maketag(u*2,L,m,lzya[u],'A');
maketag(u*2+1,m+1,R,lzym[u],'M');
maketag(u*2+1,m+1,R,lzya[u],'A');
lzym[u]=1;
lzya[u]=0;
}
int query(int l,int r,int L,int R,int u)
{
if(inRange(l,r,L,R))
return w[u];
else if(!outOfRange(l,r,L,R))
{
int m=(L+R)>>1;
pushdown(u,L,R);
return (query(l,r,L,m,u*2)+query(l,r,m+1,R,u*2+1))%p;
}
else
return 0;
}
void update(int u,int L,int R,int l,int r,long long x,int type)
{
if(inRange(l,r,L,R))
maketag(u,L,R,x,type);
else if(!outOfRange(l,r,L,R))
{
int m=(L+R)>>1;
pushdown(u,L,R);
update(u*2,L,m,l,r,x,type);
update(u*2+1,m+1,R,l,r,x,type);
pushup(u);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n,q;
cin>>n>>q>>p;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
for(int i=1;i<=q;i++)
{
long long t,x,y,k;
cin>>t;
switch(t)
{
case 1:
cin>>x>>y>>k;
update(1,1,n,x,y,k,'M');
break;
case 2:
cin>>x>>y>>k;
update(1,1,n,x,y,k,'A');
break;
case 3:
cin>>x>>y;
cout<<query(x,y,1,n,1)<<"\n";
break;
}
}
return 0;
}
回复
共 13 条回复,欢迎继续交流。
正在加载回复...