蒟蒻初学树链剖分,请求各位dalao帮忙查错
查看原帖
蒟蒻初学树链剖分,请求各位dalao帮忙查错
120017
JeffZhao楼主2020/6/26 22:11
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>

using namespace std;

const int N = 100010;

int n, m, w[N], Root, mod, xx, yy, ins, x, y, z;
int siz[N], fa[N], dep[N], son[N];
int cnt, wt[N], top[N], id[N];

inline int du(void) {
	int x = 0, f = 1; char ch = getchar();
	while (ch > '9' && ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
inline void write(int x) {
	if (x < 0) { putchar('-'); x = -x; }
	if (x > 9)write(x / 10);
	putchar(x % 10 ^ 48);
	return;
}

int head[N << 1], nx[N << 1], ver[N << 1], tot;
inline void add(int x, int y) {
	ver[++tot] = y, nx[tot] = head[x], head[x] = tot;
	return;
}

int sum[N << 2], tag[N << 2];
inline void pushdown(int k, int l, int r) {
	if (tag[k] == 0)
		return;
	int mid = (l + r) >> 1;
	tag[k << 1] += tag[k];
	sum[k << 1] += (mid - l + 1) * tag[k]; sum[k << 1] %= mod;
	tag[k << 1 | 1] += tag[k];
	sum[k << 1 | 1] += (r - mid) * tag[k]; sum[k << 1 | 1] %= mod;
	tag[k] = 0;
	return;
}
inline void build(int k, int l, int r) {
	if (l == r) { 
		sum[k] = wt[l]; 
		return; 
	}
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
	sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % mod;
	return;
}
inline void update(int k, int l, int r, int x, int y, int z) {
	if (l >= x && r <= y) {
		tag[k] += z;
		sum[k] += (r - l + 1) * z % mod;
		return;
	}
	int mid = (l + r) >> 1;
	pushdown(k, l, r);
	update(k << 1, l, mid, x, y, z);
	update(k << 1 | 1, mid + 1, r, x, y, z);
	sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % mod;
	return;
}
inline int query(int k, int l, int r, int x, int y) {
	if (l >= x && r <= y)
		return sum[k] % mod;
	int mid = (l + r) >> 1 , res = 0;
	pushdown(k, l, r);
	if (x <= mid)res += query(k << 1, l, mid, x, y);
	if (y > mid)res += query(k << 1 | 1, mid + 1, r, x, y);
	return res % mod;
}

inline void dfs1(int u, int father, int deep) {
	siz[u] = 1;
	fa[u] = father;
	dep[u] = deep;
	int maxson = -1;
	for (int i = head[u]; i; i = nx[i]) {
		int v = ver[i];
		if (v == father)continue;
		dfs1(v, u, deep + 1);
		siz[u] += siz[v];
		if (siz[v] > maxson) { maxson = siz[v]; son[u] = v; }
	}
	return;
}
inline void dfs2(int u, int topf) {
	id[u] = ++cnt;
	wt[cnt] = w[u];
	top[u] = topf;
	if (!son[u])return;
	dfs2(son[u], topf);
	for (int i = head[u]; i; i = nx[i]) {
		int v = ver[i];
		if (v == fa[u] || v == son[u])continue;
		dfs2(v, v);
	}
	return;
}

inline int qRange(int x, int y) {
	int ans = 0;
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]])swap(x, y);
		ans = (ans + query(1, 1, n, id[top[x]], id[x])) % mod;
		x = fa[top[x]];
	}
	if (dep[x] > dep[y])swap(x, y);
	ans = (ans + query(1, 1, n, id[x], id[y])) % mod;
	return ans;
}

inline int qSon(int u) { return query(1, 1, n, id[u], id[u] + siz[u] - 1) % mod; }

inline void upSon(int u, int z) { update(1, 1, n, id[u], id[u] + siz[u] - 1, z); return; }

inline void upRange(int x, int y, int z) {
	z %= mod;
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]])swap(x, y);
		update(1, 1, n, id[top[x]], id[x], z);
		x = fa[top[x]];
	}
	if (dep[x] > dep[y])swap(x, y);
	update(1, 1, n, id[x], id[y], z);
	return;
}

int main(void) {
	n = du(), m = du(), Root = du(), mod = du();
	for (int i = 1; i <= n; ++i)
		w[i] = du();
	for (int i = 1; i < n; ++i) {
		xx = du(), yy = du();
		add(xx, yy);
		add(yy, xx);
	}
	dfs1(Root, 0, 1);
	dfs2(Root, Root);
	build(1, 1, n);
	for (register int i = 1; i <= m; ++i) {
		ins = du();
		if (ins == 1) {
			x = du(), y = du(), z = du();
			upRange(x, y, z);
		}
		if (ins == 2) {
			x = du(), y = du();
			write(qRange(x, y)); 
			putchar('\n');
		}
		if (ins == 3) {
			x = du(), z = du();
			upSon(x, z);
		}
		if (ins == 4) {
			x = du();
			write(qSon(x));
			putchar('\n');
		}
	}
	return 0;
}
2020/6/26 22:11
加载中...