社区讨论
线段树2求调
学术版参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @lo7ubndq
- 此快照首次捕获于
- 2023/10/27 07:53 2 年前
- 此快照最后确认于
- 2023/10/27 07:53 2 年前
线段树2
0分求调
CPP#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000010;
ll a[maxn<<2],ans[maxn<<2],n,m,q,lazy[maxn<<2],lazy1[maxn<<2];
inline ll ls(ll p) {
return p<<1;
}
inline ll rs(ll p) {
return p<<1|1;
}
void input() {
scanf("%lld%lld%lld",&n,&m,&q);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
}
void push_up(ll p) {
ans[p]=ans[ls(p)]+ans[rs(p)];
ans[p]%=q;
}
void build(ll p,ll l,ll r)
{
lazy1[p]=1;
if(l==r) {
ans[p]=a[l];
return;
}
ll mid=(l+r)>>1;
build(ls(p),l,mid);
build(rs(p),mid+1,r);
push_up(p);
}
//-------------------------------------------------
inline void f(ll p,ll l,ll r,ll k) {
lazy[p]=lazy[p]+k;
lazy[p]%=q;
ans[p]=ans[p]+k*(r-l+1);
ans[p]%=q;
}
inline void push_down(ll p,ll l,ll r) {
ll mid=(l+r)>>1;
f(ls(p),l,mid,lazy[p]);
f(rs(p),mid+1,r,lazy[p]);
lazy[p]=0;
}
inline void update(ll L,ll R,ll l,ll r,ll p,ll k) {
if(L<=l&&r<=R) {
ans[p]+=k*(r-l+1);
ans[p]%=q;
lazy[p]+=k;
lazy[p]%=q;
return ;
}
push_down(p,l,r);
ll mid=(l+r)>>1;
if(L<=mid) update(L,R,l,mid,ls(p),k);
if(R>mid) update(L,R,mid+1,r,rs(p),k);
push_up(p);
}
//-------------------------------------------------
inline void f1(ll p,ll l,ll r,ll k) {
lazy1[p]=lazy1[p]*k;
lazy1[p]%=q;
ans[p]=ans[p]*k;
ans[p]%=q;
}
inline void push_down1(ll p,ll l,ll r) {
ll mid=(l+r)>>1;
f1(ls(p),l,mid,lazy[p]);
f1(rs(p),mid+1,r,lazy[p]);
lazy1[p]=1;
}
inline void update1(ll L,ll R,ll l,ll r,ll p,ll k) {
if(L<=l&&r<=R) {
ans[p]*=k;
ans[p]%=q;
lazy1[p]*=k;
lazy1[p]%=q;
return;
}
push_down1(p,l,r);
ll mid=(l+r)>>1;
//\if(lazy1[p]!=1||ans[p]) push_down1(p,l,r);
if(L<=mid) update1(L,R,l,mid,ls(p),k);
if(R>mid) update1(L,R,mid+1,r,rs(p),k);
push_up(p);
}
//-------------------------------------------------
ll query(ll L,ll R,ll l,ll r,ll p) {
ll res=0;
if(L<=l&&r<=R) return ans[p]%q;
ll mid=(l+r)>>1;
push_down(p,l,r);
if(L<=mid) res+=query(L,R,l,mid,ls(p)),res%=q;
if(R>mid) res+=query(L,R,mid+1,r,rs(p)),res%=q;
return res%q;
}
void work() {
build(1,1,n);
ll mac;
while(m--)
{
ll x,y,k;
scanf("%lld",&mac);
if(mac==1)
{
scanf("%lld%lld%lld",&x,&y,&k); //*
update1(x,y,1,n,1,k);
}
else if(mac==2)
{
scanf("%lld%lld%lld",&x,&y,&k);
update(x,y,1,n,1,k);
}
else
{
scanf("%lld%lld",&x,&y);
cout<<query(x,y,1,n,1)<<endl;
}
}
}
int main()
{
input();
work();
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...