CF1017G The Tree

题意

给定一棵树和 \(3\) 个操作。

  • 如果点 \(x\) 是白色,将她染红,否则对她地儿子做这个操作。
  • 将点 \(x\) 子树内所有点染白。
  • 询问 \(x\) 的颜色。

Sol

考虑对询问分块。

不难想到将当前块内的点建一棵虚树,然后再重构。

暴力建虚树即可。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <vector>
#include <bitset>
#include <cassert>
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
#define fi first
#define se second

const int N = 2e5 + 5, M = 4e5 + 5, bsk = 317;

namespace G {

array <int, N> fir;
array <int, M> nex, to;
int cnt = 1;

void add(int x, int y) {
	cnt++;
	nex[cnt] = fir[x];
	to[cnt] = y;
	fir[x] = cnt;
}

}

namespace T {

array <int, N> fir;
array <int, M> nex, to, len;
int cnt = 1;

void add(int x, int y, int z) {
	assert(x != y);
	cnt++;
	nex[cnt] = fir[x];
	to[cnt] = y;
	len[cnt] = z;
	fir[x] = cnt;
}

}

namespace Hpt {

array <int, N> fa, dep, siz, son;

void dfs1(int x) {
	siz[x] = 1;
	for (int i = G::fir[x]; i; i = G::nex[i]) {
		if (G::to[i] == fa[x]) continue;
		fa[G::to[i]] = x;
		dep[G::to[i]] = dep[x] + 1;
		dfs1(G::to[i]);
		siz[x] += siz[G::to[i]];
		if (siz[G::to[i]] > siz[son[x]]) son[x] = G::to[i];
	}
}

array <int, N> dfn, top, idx;
int cnt;

void dfs2(int x, int Mgn) {
	cnt++;
	dfn[x] = cnt;
	idx[cnt] = x;
	top[x] = Mgn;
	if (son[x]) dfs2(son[x], Mgn);
	for (int i = G::fir[x]; i; i = G::nex[i]) {
		if (G::to[i] == fa[x] || G::to[i] == son[x]) continue;
		dfs2(G::to[i], G::to[i]);
	}
}

int lca(int x, int y) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) swap(x, y);
	return x;
}

}

namespace Vit {

array <int, N> stk;
bitset <N> vis;
int top;

array <int, N> isl, agc;

int getdep(int x, int y) {
	int ans = 0;
	y = Hpt::fa[y];
	while (y != x)
		ans += !agc[y], y = Hpt::fa[y];
	return ans;
}

void build(vector <int> &isl) {
	sort(isl.begin(), isl.end(), [](int x, int y) {
		return Hpt::dfn[x] < Hpt::dfn[y];
	});
	isl.erase(unique(isl.begin(), isl.end()), isl.end());
	T::cnt = 1;
	top = 1, stk[top] = isl.front(), T::fir[isl.front()] = 0;
	vis[isl.front()] = 1;
	for (int i = 1; i < (int)isl.size(); i++) {
		int lcA = Hpt::lca(stk[top], isl[i]);
		if (lcA != stk[top]) {
			while (Hpt::dfn[stk[top - 1]] > Hpt::dfn[lcA])
				T::add(stk[top - 1], stk[top], getdep(stk[top - 1], stk[top])), top--;
			if (stk[top - 1] != lcA)
				T::fir[lcA] = 0, T::add(lcA, stk[top], getdep(lcA, stk[top])), stk[top] = lcA, vis[lcA] = 1;
			else
				T::add(stk[top - 1], stk[top], getdep(stk[top - 1], stk[top])), top--;
		}
		T::fir[isl[i]] = 0, top++, stk[top] = isl[i], vis[isl[i]] = 1;
	}
	for (int i = 1; i < top; i++)
		T::add(stk[i], stk[i + 1], getdep(stk[i], stk[i + 1]));
}

bitset <N> cur;

void rever(int x) {
	if (!agc[x]) {
		agc[x] = 1;
		return;
	}
	isl[x]++;
	for (int i = T::fir[x]; i; i = T::nex[i]) {
		if (T::len[i] > isl[x]) continue;
		rever(T::to[i]);
	}
}

void cover(int x) {
	agc[x] = isl[x] = 0;
	cur[x] = 1;
	for (int i = T::fir[x]; i; i = T::nex[i])
		T::len[i] = Hpt::dep[G::to[i]] - Hpt::dep[x], cover(T::to[i]);
}

void dfs(int x, int tp1, int tp2) {
	if (vis[x]) tp1 = isl[x], tp2 |= cur[x];
	if (!vis[x] && tp2) agc[x] = 0;
	if (!vis[x] && !agc[x] && tp1) tp1--, agc[x] = 1;
	for (int i = G::fir[x]; i; i = G::nex[i]) {
		if (G::to[i] == Hpt::fa[x]) continue;
		dfs(G::to[i], tp1, tp2);
	}
}

}

vector <pii> qrl;
vector <int> tp;

int main() {
	int n = read(), m = read();
	for (int i = 2; i <= n; i++) {
		int x = read();
		G::add(x, i), G::add(i, x);
	}
	Hpt::dfs1(1), Hpt::dfs2(1, 0);
	for (int i = 1; i <= m; i++) {
		int op = read(), x = read();
		qrl.push_back(make_pair(op, x));
		tp.push_back(x), Vit::vis[x] = 1;
		/* puts("@"); */
		if (i % bsk && i != m) continue;
		Vit::build(tp);
		for (auto [op, x] : qrl) {
			if (op == 1) Vit::rever(x);
			if (op == 2) Vit::cover(x);
			if (op == 3) puts(Vit::agc[x] ? "black" : "white");
		}
		Vit::dfs(Vit::stk[1], 0, 0);
		Vit::vis = 0, Vit::cur = 0;
		Vit::isl.fill(0);
		tp.clear(), qrl.clear();
	}
	return 0;
}
posted @ 2024-01-07 14:30  cxqghzj  阅读(2)  评论(0编辑  收藏  举报