题目链接:点击这里
解题思路:
以前做区间修改都是一种标记,可以直接标记.而现在这题既有乘法更新又有加法更新,如果再按以前的思想循序更新就肯定会出错.
所以在多重标记的情况下我们可以给它们制定一个优先级的规则,假设每次的更新都是乘法优先于加法.那么更新操作要怎么改变呢?
设add[]为加法懒惰标记,mul[]为乘法懒惰标记.
对区间rt进行加x操作:直接add[rt] += x
对区间rt进行乘x操作:原来区间值可看为 y*a + b,(y*a + b)*x = y*(a*b) + b*x(a为当前mul[rt]的值,b为当前add[rt]的值),
所以mul[rt] = mul[rt]*x,add[rt] = add[rt]*x
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
int n,m,mod,d,L,R,k;
ll sum[mx*3],mul[mx*3],add[mx*3];
void build(int l,int r,int rt)
{
mul[rt] = 1;
if(l==r){
scanf("%lld",sum+rt);
return;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
sum[rt] = (sum[rt<<1]+sum[rt<<1|1])%mod;
}
void push_down(int l,int r,int rt)
{
mul[rt<<1] = (mul[rt<<1]*mul[rt])%mod;
mul[rt<<1|1] = (mul[rt<<1|1]*mul[rt])%mod;
add[rt<<1] = (add[rt<<1]*mul[rt]+add[rt])%mod;
add[rt<<1|1] = (add[rt<<1|1]*mul[rt]+add[rt])%mod;
int mid = (l+r)>>1;
sum[rt<<1] = (sum[rt<<1]*mul[rt] + add[rt]*(mid-l+1))%mod;
sum[rt<<1|1] = (sum[rt<<1|1]*mul[rt] + add[rt]*(r-mid))%mod;
mul[rt] = 1,add[rt] = 0;
}
void update(int l,int r,int rt)
{
if(L<=l&&r<=R){
if(d==1){
mul[rt] = (mul[rt]*k)%mod;
add[rt] = (add[rt]*k)%mod;
sum[rt] = (sum[rt]*k)%mod;
}else{
add[rt] = (add[rt]+k)%mod;
sum[rt] = (sum[rt]+1ll*(r-l+1)*k)%mod;
}
return ;
}
push_down(l,r,rt);
int mid = (l+r)>>1;
if(L<=mid) update(lson);
if(R>mid) update(rson);
sum[rt] = (sum[rt<<1]+sum[rt<<1|1])%mod;
}
ll query(int l,int r,int rt)
{
if(L<=l&&r<=R) return sum[rt];
push_down(l,r,rt);
int mid = (l+r)>>1;
ll ans = 0;
if(L<=mid) ans = (ans+query(lson))%mod;
if(R>mid) ans = (ans+query(rson))%mod;
return ans;
}
int main()
{
scanf("%d%d%d",&n,&m,&mod);
build(1,n,1);
while(m--){
scanf("%d",&d);
if(d!=3){
scanf("%d%d%d",&L,&R,&k);
update(1,n,1);
}else{
scanf("%d%d",&L,&R);
printf("%lld\n",query(1,n,1));
}
}
return 0;
}