社区讨论
求助大佬
P3373【模板】线段树 2参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mi7rvswu
- 此快照首次捕获于
- 2025/11/21 02:36 4 个月前
- 此快照最后确认于
- 2025/11/21 02:36 4 个月前
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int INF=2147483647;
inline ll read()
{
ll x=0,k=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();}
while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return xk;
}
struct node{
ll v,lm,la;
}tree[400500];
ll n,m,op,x,y,k,p,a[100086];
void update(ll root)
{
tree[root].v=(tree[root<<1].v+tree[root<<1|1].v)%p;
}
void build(ll l,ll r,ll root)
{
tree[root].lm=1,tree[root].la=0;
if(l==r){
tree[root].v=a[l]%p;
return ;
}
ll mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
update(root);
}
void pushdown(ll l,ll r,ll root)
{
ll mid=(l+r)>>1;
tree[root<<1].lm=(tree[root<<1].lmtree[root].lm)%p;
tree[root<<1|1].lm=(tree[root<<1|1].lmtree[root].lm)%p;
tree[root<<1].la=(tree[root<<1].latree[root].lm+tree[root].la)%p;
tree[root<<1|1].la=(tree[root<<1|1].latree[root].lm+tree[root].la)%p;
tree[root<<1].v=(tree[root<<1].vtree[root].lm+tree[root].la*(mid-r+1));
tree[root<<1|1].v=(tree[root<<1|1].vtree[root].lm+tree[root].la(r-mid));
tree[root].lm=1,tree[root].la=0;
}
void mul(ll l,ll r,ll lnow,ll rnow,ll x,ll root)
{
if(l<=lnow&&r>=rnow)
{
tree[root].lm=(tree[root].lmx)%p;
tree[root].la=(tree[root].lax)%p;
tree[root].v=(tree[root].vx)%p;
return ;
}
pushdown(lnow,rnow,root);
ll mid=(lnow+rnow)>>1;
if(l<=mid)mul(l,r,lnow,mid,x,root<<1);
if(r>mid)mul(l,r,mid+1,rnow,x,root<<1|1);
update(root);
}
void add(ll l,ll r,ll lnow,ll rnow,ll x,ll root)
{
if(l<=lnow&&r>=rnow)
{
tree[root].la=(tree[root].la+x)%p;
tree[root].v=(tree[root].v+x(rnow-lnow+1))%p;
return ;
}
pushdown(lnow,rnow,root);
ll mid=(lnow+rnow)>>1;
if(l<=mid)add(l,r,lnow,mid,x,root<<1);
if(r>mid)add(l,r,mid+1,rnow,x,root<<1|1);
update(root);
}
ll query(ll l,ll r,ll lnow,ll rnow,ll root)
{
if(l<=lnow&&r>=rnow)
{
return tree[root].v;
}
pushdown(lnow,rnow,root);
ll mid=(lnow+rnow)>>1,ans=0;
if(l<=mid)ans=(ans+query(l,r,lnow,mid,root<<1))%p;
if(r>mid)ans=(ans+query(l,r,mid+1,rnow,root<<1|1))%p;
return ans;
}
int main()
{
n=read(),m=read(),p=read();
rep(i,1,n)a[i]=read();
build(1,n,1);
while(m--)
{
op=read(),x=read(),y=read();
if(op==1)
{
k=read();
mul(x,y,1,n,k,1);
}
else if(op==2)
{
k=read();
add(x,y,1,n,k,1);
}
else
{
ll t=query(x,y,1,n,1);
printf("%lld\n",t);
}
}
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...