FHQ Treap,但是T了捏
查看原帖
FHQ Treap,但是T了捏
364848
Bodhi楼主2023/7/5 23:52

有什么解决方法吗(已开O2)

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int R = 1e5 + 10;
int M;
struct Node
{
	int son[2], sz;
	unsigned int key;
	ll res, org, mul, add;
} t[R];
#define lc(x) t[x].son[0]
#define rc(x) t[x].son[1]
int root, tot;
mt19937 rd;
int crt(int val)
{
	t[++tot] = {
		.sz = 1,
		.key = rd(),
		.res = val,
		.org = val,
		.mul = 1,
		.add = 0};
	return tot;
}
void edit(int x, ll mul, ll add)
{
	mul %= M, add %= M;
	t[x].org = (t[x].org * mul % M + add) % M;
	t[x].res = (t[x].res * mul % M + ll(t[lc(x)].sz + t[rc(x)].sz + 1) * add % M) % M;
	t[x].mul = t[x].mul * mul % M;
	t[x].add = (t[x].add * mul % M + add) % M;
}
void pushdown(int x)
{
	if (lc(x))
		edit(lc(x), t[x].mul, t[x].add);
	if (rc(x))
		edit(rc(x), t[x].mul, t[x].add);
	t[x].mul = 1, t[x].add = 0;
}
void pushup(int x)
{
	t[x].sz = t[lc(x)].sz + t[rc(x)].sz + 1;
	t[x].res = (t[lc(x)].res + t[rc(x)].res + t[x].org) % M;
}
void split(int k, int sz, int &x, int &y)
{
	if (k == 0)
	{
		x = y = 0;
		return;
	}
	pushdown(k);
	if (t[lc(k)].sz + 1 <= sz)
	{
		x = k;
		split(rc(k), sz - (t[lc(x)].sz + 1), rc(x), y);
	}
	else
	{
		y = k;
		split(lc(k), sz, x, lc(y));
	}
	pushup(k);
}
int merge(int x, int y)
{
	if (!x || !y)
		return x | y;
	if (t[x].key < t[y].key)
	{
		pushdown(x);
		rc(x) = merge(rc(x), y);
		pushup(x);
		return x;
	}
	else
	{
		pushdown(y);
		lc(y) = merge(x, lc(y));
		pushup(y);
		return y;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n, m, x, y, a, b, c;
	ll k;
	cin >> n >> m >> M;
	for (int j = 1; j <= n; ++j)
	{
		cin >> x;
		root = merge(root, crt(x % M));
	}
	char op;
	while (m--)
	{
		cin >> op >> x >> y;
		split(root, x - 1, a, b);
		split(b, y - x + 1, b, c);
		if (op == '1') // mul
		{
			cin >> k;
			edit(b, k, 0);
		}
		else if (op == '2') // add
		{
			cin >> k;
			edit(b, 1, k);
		}
		else
		{
			pushdown(b);
			cout << t[b].res % M << '\n';
		}
		root = merge(merge(a, b), c);
	}
	return 0;
}

2023/7/5 23:52
加载中...