You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.3 KiB
99 lines
2.3 KiB
#include <cstdio>
|
|
#include <cstring>
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
typedef long long LL;
|
|
|
|
//线段树宏定义
|
|
#define ls u << 1
|
|
#define rs u << 1 | 1
|
|
|
|
const int N = 100010;
|
|
|
|
// n:N 个非负整数 p:数模 P 的值 m:操作总数
|
|
int n, p, m;
|
|
int w[N];
|
|
|
|
struct Node {
|
|
int l, r;
|
|
LL sum, add, mul;
|
|
} tr[N << 2];
|
|
|
|
void pushup(int u) {
|
|
tr[u].sum = (tr[u << 1].sum + tr[u << 1 | 1].sum) % p; //更新sum和信息
|
|
}
|
|
|
|
//计算函数
|
|
void eval(Node &t, LL add, LL mul) {
|
|
t.sum = (t.sum * mul + (t.r - t.l + 1) * add) % p;
|
|
t.mul = t.mul * mul % p;
|
|
t.add = (t.add * mul + add) % p;
|
|
}
|
|
|
|
void pushdown(int u) {
|
|
if (tr[u].add || tr[u].mul != 1) {
|
|
eval(tr[ls], tr[u].add, tr[u].mul); //处理左儿子
|
|
eval(tr[rs], tr[u].add, tr[u].mul); //处理右儿子
|
|
tr[u].add = 0, tr[u].mul = 1; //清空懒标记
|
|
}
|
|
}
|
|
|
|
void build(int u, int l, int r) {
|
|
tr[u] = {l, r, w[r], 0, 1};
|
|
if (l == r) return;
|
|
int mid = (l + r) >> 1;
|
|
build(ls, l, mid), build(rs, mid + 1, r);
|
|
pushup(u);
|
|
}
|
|
|
|
void modify(int u, int l, int r, int add, int mul) {
|
|
if (tr[u].l >= l && tr[u].r <= r) {
|
|
eval(tr[u], add, mul);
|
|
return;
|
|
}
|
|
|
|
pushdown(u);
|
|
int mid = tr[u].l + tr[u].r >> 1;
|
|
if (l <= mid) modify(ls, l, r, add, mul);
|
|
if (r > mid) modify(rs, l, r, add, mul);
|
|
pushup(u);
|
|
}
|
|
|
|
int query(int u, int l, int r) {
|
|
if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
|
|
|
|
pushdown(u);
|
|
int mid = tr[u].l + tr[u].r >> 1;
|
|
int sum = 0;
|
|
if (l <= mid) sum = query(ls, l, r);
|
|
if (r > mid) sum = (sum + query(rs, l, r)) % p;
|
|
return sum;
|
|
}
|
|
|
|
int main() {
|
|
//加快读入
|
|
ios::sync_with_stdio(false), cin.tie(0);
|
|
|
|
cin >> n >> p;
|
|
for (int i = 1; i <= n; i++) cin >> w[i];
|
|
|
|
//构建线段树,root=1,l=1,r=n
|
|
build(1, 1, n);
|
|
|
|
cin >> m;
|
|
while (m--) {
|
|
int t, l, r, d;
|
|
cin >> t >> l >> r;
|
|
if (t == 1) { //乘
|
|
cin >> d;
|
|
modify(1, l, r, 0, d);
|
|
} else if (t == 2) { //加
|
|
cin >> d;
|
|
modify(1, l, r, d, 1);
|
|
} else //查
|
|
printf("%d\n", query(1, l, r));
|
|
}
|
|
return 0;
|
|
} |