线段树 30 分求助,只过了 1, 3, 4,开了 long long 并取膜了
查看原帖
线段树 30 分求助,只过了 1, 3, 4,开了 long long 并取膜了
204619
wwhOvO楼主2022/8/9 17:00

看了一圈 30 分的求助帖发现都解决不了我的问题,求大佬们帮忙看看

#include <iostream>
#include <cstdio>
using namespace std;

const int MN = 100005;
const int MS = 4 * MN;
typedef long long LL;

int n, m, p;
LL sum[MS], tgMul[MS], tgAdd[MS], a[MN];

#define li (1 << i)
#define ri (li | 1)
#define mid ((l + r) >> 1)
#define ls li, l, mid
#define rs ri, mid + 1, r
#define len (r - l + 1)
void Pushup(int i) { sum[i] = (sum[li] % p + sum[ri] % p) % p; }
void P(int i, int j, int l, int r) { 
	sum[i] = (sum[i] * tgMul[j] % p + tgAdd[j] * (LL)len % p) % p;
	tgAdd[i] = tgAdd[i] * tgMul[j] % p + tgAdd[j] % p;
	tgMul[i] *= tgMul[j] % p;
}
void Pushdown(int i, int l, int r) { P(li, i, l, mid), P(ri, i, mid + 1, r), tgMul[i] = 1LL, tgAdd[i] = 0; }
void Build(int i, int l, int r) {
	tgMul[i] = 1LL;
	if (l == r) {
		sum[i] = a[l] % p;
		return ;
	}
	Build(ls); Build(rs);
	Pushup(i);
} 
void MdfAdd(int i, int l, int r, int x, int y, LL k) {
	if (r < x || l > y) return ;
	if (x <= l && r <= y) { tgAdd[i] += k % p; sum[i] += k * (LL)len % p; return ;}
	Pushdown(i, l, r);
	MdfAdd(ls, x, y, k);
	MdfAdd(rs, x, y, k);
	Pushup(i);
}
void MdfMul(int i, int l, int r, int x, int y, LL k) {
	if (r < x || l > y) return ;
	if (x <= l && r <= y) { tgMul[i] *= k % p; tgAdd[i] *= k % p; sum[i] *= k % p; return ; }
	Pushdown(i, l, r);
	MdfMul(ls, x, y, k);
	MdfMul(rs, x, y, k);
	Pushup(i);
}
LL Query(int i, int l, int r, int x, int y) {
	if (r < x || l > y) return 0;
	if (x <= l && r <= y) return sum[i] % p;
	Pushdown(i, l, r);
	LL ans = Query(ls, x, y) % p + Query(rs, x, y) % p;
	return ans % p;
}
#undef li
#undef ri
#undef mid
#undef ls
#undef rs
#undef len

void Pt() {
	for (int i = 1; i <= n; i++)
		cout << Query(1, 1, n, i, i) % p << ' ';
	cout << '\n';
} 

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m >> p;
	for (int i = 1; i <= n; i++) cin >> a[i];
	Build(1, 1, n);
	while (m--) {
		int f, x, y; LL k;
		cin >> f >> x >> y;
		if (f != 3) {
			cin >> k;
			if (f == 1) MdfMul(1, 1, n, x ,y, k);
			else MdfAdd(1, 1, n, x ,y, k);
		} else cout << Query(1, 1, n, x, y) % p << '\n';
		
//		Pt();
	}
	return 0;
}
2022/8/9 17:00
加载中...