【BZOJ2243】[SDOI2011]染色【树链剖分】

【题目链接】

很容易想到树剖,然后主要是线段树。

sum记录一段区间内的颜色子段个数,lx和rx分别记录左端点和右端点的颜色是什么。

然后就差不多了。

注意树剖爬的时候也得注意节点颜色。

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 100005, maxm = maxn;

int n, m, head[maxn], cnt, w[maxn];

struct _edge {
	int v, next;
} g[maxm << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

int son[maxn], size[maxn], top[maxn], pre[maxn], dfn[maxn], depth[maxn], clo;

inline void dfs1(int x) {
	size[x] = 1; son[x] = 0;
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ pre[x]) {
		pre[g[i].v] = x;
		depth[g[i].v] = depth[x] + 1;
		dfs1(g[i].v);
		size[x] += size[g[i].v];
		if(size[g[i].v] > size[son[x]]) son[x] = g[i].v;
	}
}

inline void dfs2(int x, int tp) {
	top[x] = tp; dfn[x] = ++clo;
	if(son[x]) dfs2(son[x], tp);
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ pre[x] && g[i].v ^ son[x])
		dfs2(g[i].v, g[i].v);
}

int val[maxn], sum[maxn << 2], lx[maxn << 2], rx[maxn << 2], setv[maxn << 2];

inline void pushup(int p) {
	sum[p] = sum[p << 1] + sum[p << 1 | 1] - (rx[p << 1] == lx[p << 1 | 1]);
	lx[p] = lx[p << 1];
	rx[p] = rx[p << 1 | 1];
}

inline void pushdown(int p) {
	if(setv[p]) {
		lx[p << 1] = rx[p << 1] = lx[p << 1 | 1] = rx[p << 1 | 1] = setv[p << 1] = setv[p << 1 | 1] = setv[p];
		sum[p << 1] = sum[p << 1 | 1] = 1;
		setv[p] = 0;
	}
}

inline void build(int p, int l, int r) {
	if(l == r) {
		sum[p] = 1;
		lx[p] = rx[p] = val[l];
		return;
	}
	int mid = l + r >> 1;
	build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r);
	pushup(p);
}

int LX, RX;

inline int query(int p, int l, int r, int x, int y) {
	if(l == x) LX = lx[p];
	if(r == y) RX = rx[p];
	if(x <= l && r <= y) return sum[p];
	int mid = l + r >> 1, ans = 0, x1 = 0, x2 = 0;
	pushdown(p);
	if(x <= mid) ans += query(p << 1, l, mid, x, y), x1 = rx[p << 1];
	if(y > mid) ans += query(p << 1 | 1, mid + 1, r, x, y), x2 = lx[p << 1 | 1];
	return ans - (x1 == x2 && x1 != 0);
}

inline void change(int p, int l, int r, int x, int y, int c) {
	if(x <= l && r <= y) {
		sum[p] = 1;
		lx[p] = rx[p] = setv[p] = c;
		return;
	}
	int mid = l + r >> 1;
	pushdown(p);
	if(x <= mid) change(p << 1, l, mid, x, y, c);
	if(y > mid) change(p << 1 | 1, mid + 1, r, x, y, c);
	pushup(p);
}

inline int querychain(int u, int v) {
	int topu = top[u], topv = top[v], ans = 0;
	int ux = 0, vx = 0; LX = RX = 0;
	for(; topu != topv; topu = top[u = pre[topu]]) {
		if(depth[topu] < depth[topv]) swap(topu, topv), swap(u, v), swap(ux, vx);
		ans += query(1, 1, clo, dfn[topu], dfn[u]) - (RX == ux);
		ux = LX;
	}
	if(depth[u] < depth[v]) swap(u, v), swap(ux, vx);
	ans += query(1, 1, clo, dfn[v], dfn[u]) - (ux == RX) - (vx == LX);
	return ans;
}

inline void changechain(int u, int v, int c) {
	int topu = top[u], topv = top[v], ans = 0;
	for(; topu != topv; topu = top[u = pre[topu]]) {
		if(depth[topu] < depth[topv]) swap(topu, topv), swap(u, v);
		change(1, 1, clo, dfn[topu], dfn[u], c);
	}
	if(depth[u] < depth[v]) swap(u, v);
	change(1, 1, clo, dfn[v], dfn[u], c);
}

int main() {
	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) w[i] = iread() + 1, head[i] = -1; cnt = 0;
	for(int i = 1; i < n; i++) {
		int u = iread(), v = iread();
		add(u, v); add(v, u);
	}

	depth[1] = 1;
	dfs1(1); dfs2(1, 1);

	for(int i = 1; i <= n; i++) val[dfn[i]] = w[i];
	build(1, 1, clo);

	while(m--) {
		char ch = getchar(); for(; ch != 'Q' && ch != 'C'; ch = getchar());
		if(ch == 'Q') {
			int u = iread(), v = iread();
			printf("%d\n", querychain(u, v));
		}
		else if(ch == 'C') {
			int u = iread(), v = iread(), c = iread(); c++;
			changechain(u, v, c);
		}
	}
	return 0;
}

附mkdata和暴力。

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <ctime>

inline int rd(int x) {
	return rand() % x + 1;
}

int main() {
	freopen("orz.in", "w", stdout);
	srand(time(0));
	int n = 10000, m = 10000, c = 10000;
	printf("%d %d\n", n, m);
	for(int i = 1; i <= n; i++) printf("%d ", rd(c));
	printf("\n");
	for(int i = 1; i < n; i++) {
		printf("%d %d\n", rd(i), i + 1);
	}
	for(int i = 1; i <= m; i++) {
		int opt = rd(2);
		if(opt == 1) printf("Q %d %d\n", rd(n), rd(n));
		else printf("C %d %d %d\n", rd(n), rd(n), rd(c));
	}
	return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 100005, maxm = 100005;

int n, m, head[maxn], cnt, pre[maxn], val[maxn], depth[maxn];

struct _edge {
	int v, next;
} g[maxm << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

inline void dfs(int x) {
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ pre[x]) {
		pre[g[i].v] = x;
		depth[g[i].v] = depth[x] + 1;
		dfs(g[i].v);
	}
}

inline int query(int u, int v) {
	int ans = 1;
	if(depth[u] < depth[v]) swap(u, v);
	for(; depth[u] != depth[v]; u = pre[u]) {
		if(val[u] != val[pre[u]]) ans++;
	}
	for(; u != v; u = pre[u], v = pre[v]) {
		if(val[u] != val[pre[u]]) ans++;
		if(val[v] != val[pre[v]]) ans++;
	}
	return ans;
}

inline void change(int u, int v, int c) {
	if(depth[u] < depth[v]) swap(u, v);
	for(; depth[u] != depth[v]; u = pre[u]) val[u] = c;
	for(; u != v; u = pre[u], v = pre[v]) val[u] = val[v] = c;
	val[u] = val[v] = c;
}

int main() {
	freopen("orz.in", "r", stdin); freopen("orz.ans", "w", stdout);
	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) head[i] = -1, val[i] = iread(); cnt = 0;
	for(int i = 1; i < n; i++) {
		int u = iread(), v = iread();
		add(u, v); add(v, u);
	}

	depth[1] = 1;
	dfs(1);

	while(m--) {
		char ch = getchar(); for(; ch != 'Q' && ch != 'C'; ch = getchar());
		if(ch == 'Q') {
			int u = iread(), v = iread();
			printf("%d\n", query(u, v));
		}
		else if(ch == 'C') {
			int u = iread(), v = iread(), c = iread();
			change(u, v, c);
		}
	}
	return 0;
}

话说随机数据这个暴力跑的飞快。

你可能感兴趣的:(树链剖分)