【BZOJ1103】[POI2007]大都市meg【树链剖分 / 树状数组 + dfs序】

【题目链接】

想都没想直接树剖去了...看了题解发现树状数组维护dfs序也可以做。


树剖:

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

using namespace std;

const int maxn = 250005;

int n, head[maxn], cnt;

struct _edge {
	int v, next;
} g[maxn];

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 pre[maxn], son[maxn], size[maxn], dfn[maxn], clo, top[maxn];
int tr[maxn << 2];

inline void dfs(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;
		dfs(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);
}

inline void pushup(int p) {
	tr[p] = tr[p << 1] + tr[p << 1 | 1];
}

inline void build(int p, int l, int r) {
	if(l == r) {
		if(l == 1) tr[p] = 0;
		else tr[p] = 1;
		return;
	}
	int mid = l + r >> 1;
	build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r);
	pushup(p);
}

inline int query(int p, int l, int r, int x, int y) {
	if(x <= l && r <= y) return tr[p];
	int mid = l + r >> 1, res = 0;
	if(x <= mid) res += query(p << 1, l, mid, x, y);
	if(y > mid) res += query(p << 1 | 1, mid + 1, r, x, y);
	return res;
}

inline int querychain(int u) {
	int res = 0;
	for(; u; u = pre[top[u]])
		res += query(1, 1, clo, dfn[top[u]], dfn[u]);
	return res;
}

inline void change(int p, int l, int r, int x, int c) {
	if(l == r && r == x) {
		tr[p] = c;
		return;
	}
	int mid = l + r >> 1;
	if(x <= mid) change(p << 1, l, mid, x, c);
	else change(p << 1 | 1, mid + 1, r, x, c);
	pushup(p);
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;

	for(int i = 1; i < n; i++) {
		int u = iread(), v = iread();
		if(u > v) swap(u, v);
		add(u, v);
	}
	dfs(1); dfs2(1, 1);
	build(1, 1, clo);
	for(int T = iread(); T; ) {
		char ch = getchar(); for(; ch != 'W' && ch != 'A'; ch = getchar());
		if(ch == 'W') {
			T--;
			int u = iread();
			printf("%d\n", querychain(u));
		}
		else if(ch == 'A') {
			int u = iread(), v = iread();
			if(u > v) swap(u, v);
			change(1, 1, clo, dfn[v], 0);
		}
	}
	return 0;
}

树状数组 + dfs序:

比树剖快2s...

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

using namespace std;

const int maxn = 250005, maxm = maxn << 1;

int n, m, head[maxn], cnt, st[maxn], ed[maxn], clo, pre[maxn], tr[maxm];

struct _edge {
	int v, next;
} g[maxn];

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 addedge(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

inline void add(int x, int c) {
	for(; x <= m; x += x & -x) tr[x] += c;
}

inline int sum(int x) {
	int res = 0;
	for(; x; x -= x & -x) res += tr[x];
	return res;
}

inline void dfs(int x) {
	st[x] = ++clo;
	add(clo, 1);
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ pre[x]) {
		pre[g[i].v] = x;
		dfs(g[i].v);
	}
	ed[x] = ++clo;
	add(clo, -1);
}

int main() {
	n = iread(); m = n << 1;
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;

	for(int i = 1; i < n; i++) {
		int u = iread(), v = iread();
		if(u > v) swap(u, v);
		addedge(u, v);
	}
	dfs(1);
	for(int T = iread(); T; ) {
		char ch = getchar(); for(; ch != 'W' && ch != 'A'; ch = getchar());
		if(ch == 'W') {
			T--;
			int u = iread();
			printf("%d\n", sum(st[u]) - 1);
		}
		else if(ch == 'A') {
			int u = iread(), v = iread();
			if(u > v) swap(u, v);
			add(st[v], -1); add(ed[v], 1);
		}
	}
	return 0;
}


你可能感兴趣的:(【BZOJ1103】[POI2007]大都市meg【树链剖分 / 树状数组 + dfs序】)