50pts 求条
查看原帖
50pts 求条
879904
WorldMachine楼主2024/4/3 10:46
#include <bits/stdc++.h>
using namespace std;
#define int unsigned int
const int MAXN = 100005, mod = 51061;
int n, m;
struct LCT {
	int ch[MAXN][2], fa[MAXN], rev[MAXN], siz[MAXN], w[MAXN], val[MAXN], add[MAXN], mul[MAXN];
	void upd1(int x, int c) {
		val[x] = (val[x] + c * siz[x]) % mod;
		add[x] = (add[x] + c) % mod;
		w[x] = (w[x] + c) % mod;
	}
	void upd2(int x, int c) {
		val[x] = val[x] * c % mod;
		add[x] = add[x] * c % mod;
		mul[x] = mul[x] * c % mod;
		w[x] = w[x] * c % mod;
	}
	void pushdown(int x) {
		if (mul[x] != 1) upd2(ch[x][0], mul[x]), upd2(ch[x][1], mul[x]), mul[x] = 1;
		if (add[x]) upd1(ch[x][0], add[x]), upd1(ch[x][1], add[x]), add[x] = 0;
		if (rev[x]) {
			rev[x] ^= 1, rev[ch[x][0]] ^= 1, rev[ch[x][1]] ^= 1;
			swap(ch[x][0], ch[x][1]);
		}
	}
	void pushup(int x) {
		siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
		val[x] = (val[ch[x][0]] + val[ch[x][1]] + w[x]) % mod;
	}
	bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
	void rotate(int x) {
		int y = fa[x], z = fa[y], r = ch[y][0] == x;
		if (!isroot(y)) ch[z][ch[z][0] != y] = x;
		fa[x] = z, fa[y] = x, fa[ch[x][r]] = y;
		ch[y][r ^ 1] = ch[x][r], ch[x][r] = y;
		pushup(y), pushup(x);
	}
	void update(int x) {
		if (!isroot(x)) update(fa[x]);
		pushdown(x);
	}
	void splay(int x) {
		update(x);
		int y, z;
		while (!isroot(x)) {
			y = fa[x], z = fa[y];
			if (!isroot(y)) {
				if (ch[y][0] == x ^ ch[z][0] == y) rotate(x);
				else rotate(y);
			}
			rotate(x);
		}
		pushup(x);
	}
	void access(int x) {
		int t = 0;
		while (x) {
			splay(x), ch[x][1] = t, pushup(x);
			t = x, x = fa[x];
		}
	}
	void makert(int x) { access(x), splay(x), rev[x] ^= 1; }
	void split(int x, int y) { makert(x), access(y), splay(y); }
	int find(int x) {
		access(x), splay(x);
		while (ch[x][0]) x = ch[x][0];
		splay(x);
		return x;
	}
	void link(int x, int y) {
		makert(x);
		if (find(y) != x) fa[x] = y, pushup(y);
	}
	void cut(int x, int y) {
		makert(x);
		if (find(y) == x && fa[y] == x && !ch[y][0]) fa[y] = ch[x][1] = 0, pushup(x);
	}
} t;
signed main() {
	scanf("%u%u", &n, &m);
	for (int i = 1; i <= n; i++) t.siz[i] = t.w[i] = t.mul[i] = 1;
	for (int i = 1, u, v; i <= n; i++) {
		scanf("%u%u", &u, &v);
		t.link(u, v);
	}
	char s[5] = {};
	int a, b, c, d;
	while (m--) {
		scanf("%s%u%u", s, &a, &b);
		if (s[0] == '+') {
			scanf("%u", &c);
			t.split(a, b);
			t.upd1(b, c);
		} else if (s[0] == '-') {
			scanf("%u%u", &c, &d);
			t.cut(a, b), t.link(c, d);
		} else if (s[0] == '*') {
			scanf("%u", &c);
			t.split(a, b);
			t.upd2(b, c);
		} else {
			t.split(a, b);
			printf("%u\n", t.val[b]);
		}
	}
}
2024/4/3 10:46
加载中...