【BZOJ3862】Little Devil I【树链剖分】【线段树】

【题目链接】

一棵树,每个边有黑白两种颜色,要支持三种操作

1 一条链上的颜色取反

2 只有一个顶点在一条链上的边的颜色取反

3 查询一条链上黑色边的个数


两个线段树,分别维护重链的颜色,轻链的颜色。

重链的两个端点需要分别讨论。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 100005;

int n, head[maxn], cnt, pre[maxn], depth[maxn], size[maxn], son[maxn], top[maxn], id[maxn], clo;

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

struct _segtr {
	int sum[maxn << 2];
	bool rev[maxn << 2];

	inline void pushdown(int p, int l, int mid, int r) {
		if(rev[p]) {
			sum[p << 1] = mid - l + 1 - sum[p << 1];
			sum[p << 1 | 1] = r - mid - sum[p << 1 | 1];
			rev[p << 1] ^= 1; rev[p << 1 | 1] ^= 1;
			rev[p] = 0;
		}
	}

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

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

	void change(int p, int l, int r, int x, int y) {
		if(x <= l && r <= y) {
			sum[p] = r - l + 1 - sum[p];
			rev[p] ^= 1;
			return;
		}
		int mid = l + r >> 1;
		pushdown(p, l, mid, r);
		if(x <= mid) change(p << 1, l, mid, x, y);
		if(y > mid) change(p << 1 | 1, mid + 1, r, x, y);
		pushup(p);
	}

	int query(int p, int l, int r, int x, int y) {
		if(x <= l && r <= y) return sum[p];
		int mid = l + r >> 1, res = 0;
		pushdown(p, l, mid, r);
		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;
	}
} W, L;

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 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) {
	id[x] = ++clo; top[x] = tp;
	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 changechain(int u, int v, int k) {
	int topu = top[u], topv = top[v];
	for(; topu != topv; topu = top[u = pre[topu]]) {
		if(depth[topu] < depth[topv]) swap(u, v), swap(topu, topv);
		if(k) {
			L.change(1, 1, n, id[topu], id[u]);
			W.change(1, 1, n, id[topu], id[topu]);
			if(son[u]) W.change(1, 1, n, id[son[u]], id[son[u]]);
		} else
			W.change(1, 1, n, id[topu], id[u]);
	}
	if(depth[u] < depth[v]) swap(u, v);
	if(k) {
		L.change(1, 1, n, id[v], id[u]);
		W.change(1, 1, n, id[v], id[v]);
		if(son[u]) W.change(1, 1, n, id[son[u]], id[son[u]]);
	} else {
		if(u != v) W.change(1, 1, n, id[son[v]], id[u]);
	}
}

inline int querychain(int u, int v) {
	int topu = top[u], topv = top[v], res = 0;
	for(; topu != topv; topu = top[u = pre[topu]]) {
		if(depth[topu] < depth[topv]) swap(u, v), swap(topu, topv);
		if(topu != u) res += W.query(1, 1, n, id[son[topu]], id[u]);
		res += W.query(1, 1, n, id[topu], id[topu]) ^ L.query(1, 1, n, id[pre[topu]], id[pre[topu]]);
	}
	if(depth[u] < depth[v]) swap(u, v);
	if(u != v) res += W.query(1, 1, n, id[son[v]], id[u]);
	return res;
}

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

		dfs1(1);
		dfs2(1, 1);
		W.build(1, 1, n);
		L.build(1, 1, n);

		for(int m = iread(); m; m--) {
			int op = iread(), u = iread(), v = iread();
			if(op == 3) printf("%d\n", querychain(u, v));
			else changechain(u, v, --op);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值