线段树,延迟标记
本题要点:
1、同时对某个区间 [L, R] 的每个元素,加上 d, 乘上d。定义结构体时候,加上 add, mul 标记。
struct segTree
{
int l, r;
long long sum;
long long add, mul; //区间的加标记,乘标记
}
2、细节实现:
子树的sum、mulv、addv值分别乘上当前节点的mulv值;
当前节点的mulv值还原,即置为1;
子树的addv值加上当前节点的addv值;
子树的sum值加上(子树包含元素数量*当前节点的addv值);
当前节点的addv值还原,即置为0。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MaxN = 100010;
int n, m;
long long mod;
long long a[MaxN];
struct segTree
{
int l, r;
long long sum, add, mul;
}tree[MaxN * 4];
void build(int p, int l, int r)
{
tree[p].l = l, tree[p].r = r;
if(l == r)
{
tree[p].sum = a[l] % mod, tree[p].add = 0, tree[p].mul = 1;
return;
}
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
tree[p].sum = (tree[p * 2].sum + tree[p * 2 + 1].sum) % mod;
tree[p].add = 0, tree[p].mul = 1;
}
void spread(int p) // 从节点p向下递归
{
tree[p].mul %= mod;
if(tree[p].mul != 1)
{
tree[p * 2].sum = (tree[p * 2].sum * tree[p].mul) % mod;
tree[p * 2 + 1].sum = (tree[p * 2 + 1].sum * tree[p].mul) % mod;
tree[p * 2].add = (tree[p * 2].add * tree[p].mul) % mod;
tree[p * 2 + 1].add = (tree[p * 2 + 1].add * tree[p].mul) % mod;
tree[p * 2].mul = (tree[p * 2].mul * tree[p].mul) % mod;
tree[p * 2 + 1].mul = (tree[p * 2 + 1].mul * tree[p].mul) % mod;
tree[p].mul = 1;
}
tree[p].add %= mod;
if(tree[p].add)
{
int ld = tree[p * 2].r - tree[p * 2].l + 1;
int rd = tree[p * 2 + 1].r - tree[p * 2 + 1].l + 1;
tree[p * 2].sum = (tree[p * 2].sum + tree[p].add * ld) % mod;
tree[p * 2 + 1].sum = (tree[p * 2 + 1].sum + tree[p].add * rd) % mod;
tree[p * 2].add = (tree[p * 2].add + tree[p].add) % mod;
tree[p * 2 + 1].add = (tree[p * 2 + 1].add + tree[p].add) % mod;
tree[p].add = 0;
}
}
void change(int p, int l, int r, int d, bool flag)
{
if(l <= tree[p].l && r >= tree[p].r)
{
if(flag) //加上 d
{
tree[p].sum = (tree[p].sum + (long long)d * (tree[p].r - tree[p].l + 1)) % mod;
tree[p].add = (tree[p].add + d) % mod;
}else{
tree[p].sum = (tree[p].sum * (d % mod)) % mod;
tree[p].mul = (tree[p].mul * d) % mod;
tree[p].add = (tree[p].add * d) % mod;
}
return;
}
spread(p);
int mid = (tree[p].l + tree[p].r) / 2;
if(l <= mid)
{
change(p * 2, l, r, d, flag);
}
if(r > mid)
{
change(p * 2 + 1, l, r, d, flag);
}
tree[p].sum = (tree[2 * p].sum + tree[2 * p + 1].sum) % mod;
}
long long ask(int p, int l, int r)
{
if(l <= tree[p].l && r >= tree[p].r)
{
return tree[p].sum % mod;
}
spread(p);
int mid = (tree[p].l + tree[p].r) / 2;
long long val = 0;
if(l <= mid)
{
val = (val + ask(p * 2, l, r)) % mod;
}
if(r > mid)
{
val = (val + ask(p * 2 + 1, l, r)) % mod;
}
return val;
}
void solve()
{
build(1, 1, n);
int cmd, x, y, d;
for(int i = 0; i < m; ++i)
{
scanf("%d", &cmd);
if(1 == cmd) // 乘
{
scanf("%d%d%d", &x, &y, &d);
change(1, x, y, d, 0);
}else if(2 == cmd){
scanf("%d%d%d", &x, &y, &d);
change(1, x, y, d, 1);
}else{
scanf("%d%d", &x, &y);
printf("%lld\n", ask(1, x, y) % mod);
}
}
}
int main()
{
scanf("%d%d%lld", &n, &m, &mod);
for(int i = 1; i <= n; ++i)
{
scanf("%lld", &a[i]);
}
solve();
return 0;
}
/*
5 5 38
1 5 4 2 3
2 1 4 1
3 2 5
1 2 4 2
2 3 5 5
3 1 4
*/
/*
17
2
*/