社区讨论
线段树 0分求调 悬赏一关
P3373【模板】线段树 2参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mhjuklcc
- 此快照首次捕获于
- 2025/11/04 08:45 4 个月前
- 此快照最后确认于
- 2025/11/04 08:45 4 个月前
CPP
#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
int n,op,l,r,k,a[4100000],q,mod;
struct node
{
int add,dat,l,r,abb;
}tree[4100000];
void build(int l,int r,int p)
{
tree[p].l=l;
tree[p].r=r;
tree[p].abb=1;
if(l==r)
{
tree[p].dat=a[l]%mod;
return;
}
int mid=(l+r)/2;
build(p*2,l,mid);
build(p*2+1,r,mid+1);
tree[p].dat=(tree[p*2].dat+tree[p*2+1].dat)%mod;
}
int down(int p)
{
tree[p*2].dat=(tree[p].abb*tree[p*2].dat+((tree[p*2].r-tree[p*2].l+1)*tree[p].add)%mod)%mod;
tree[p*2+1].dat=(tree[p].abb*tree[p*2+1].dat+((tree[p*2+1].r-tree[p*2+1].l+1)*tree[p].add)%mod)%mod;
tree[p*2].abb=(tree[p*2].abb*tree[p].abb)%mod;
tree[p*2+1].abb=(tree[p*2+1].abb*tree[p].abb)%mod;
tree[p*2].add=(tree[p*2].add*tree[p].abb+tree[p].add)%mod;
tree[p*2+1].add=(tree[p*2+1].add*tree[p].abb+tree[p].add)%mod;
tree[p].abb=1;
tree[p].add=0;
}
void update(int l,int r,int p,int k)
{
if(tree[p].l>=l&&tree[p].r<=r)
{
tree[p].dat=(tree[p].dat+(tree[p].r-tree[p].l+1)*k)%mod;
tree[p].add=(tree[p].add+k)%mod;
return ;
}
down(p);
tree[p].dat=(tree[p*2].dat+tree[p*2+1].dat)%mod;
if(tree[p*2].r>=l) update(p*2,l,r,k);
if(tree[p*2+1].r>=l) update(p*2+1,l,r,k);
tree[p].dat=(tree[p*2].dat+tree[p*2+1].dat)%mod;
}
void hl(int l,int r,int k,int p)
{
if(tree[p].l>=l&&tree[p].r<=r)
{
tree[p].dat=(tree[p].dat*k)%mod;
tree[p].add=(tree[p].add*k)%mod;
tree[p].abb=(tree[p].abb*k)%mod;
return ;
}
down(p);
tree[p].dat=(tree[p*2].dat+tree[p*2+1].dat)%mod;
if(tree[p*2].r>=l) hl(p*2,l,r,k);
if(tree[p*2+1].l<=r) hl(p*2+1,l,r,k);
tree[p].dat=(tree[p*2].dat+tree[p*2+1].dat)%mod;
}
int ask(int l,int r,int p)
{
if(tree[p].l>=l&&tree[p].r<=r)
{
return tree[p].dat;
}
down(p);
int ans=0;
if(tree[p*2].r>=l) ans+=ask(p*2,l,r)%mod;
if(tree[p*2+1].l<=r) ans+=ask(p*2+1,l,r)%mod;
return ans;
}
signed main()
{
cin>>n>>q>>mod;
for(int i=1;i<=n;++i) cin>>a[i];
build(1,1,n);
while(mod--){
cin>>op>>l>>r;
if(op==1)
{
cin>>k;
hl(1,l,r,k);
}
else if(op==2)
{
cin>>k;
update(1,l,r,k);
}
else if(op==3)
{
cout<<ask(1,l,r)<<endl;
}
}
return 0;
}
//全部点RE,样例过不去。
回复
共 5 条回复,欢迎继续交流。
正在加载回复...