【CodeChef】Adi and the Tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/84922322

【题目链接】

【思路要点】

  • 首先,一条边不会在一种方案中被计算 x   ( x > 1 ) x\ (x>1) 次,否则我们可以构造出一种只计算这条边 x % 2 x\%2 次的方案,方案会变优。
  • 考虑一条边何时会被计算,显然当其两侧的点数均为奇数时,这条边会被计算。
  • 由于题目保证了任意时刻总点数为偶数,我们只需要计算子树和为奇数的点数即可。
  • 树链剖分 + 线段树维护之,时间复杂度 O ( N + M L o g 2 N ) O(N+MLog^2N)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct SegmentTree {
	struct Node {
		int lc, rc;
		int sum[2];
		bool tag;
	} a[MAXN * 2];
	int n, size, root;
	void update(int root) {
		a[root].sum[0] = a[root].sum[1] = 0;
		int tmp = a[root].lc;
		a[root].sum[0] += a[tmp].sum[0];
		a[root].sum[1] += a[tmp].sum[1];
		tmp = a[root].rc;
		a[root].sum[0] += a[tmp].sum[0];
		a[root].sum[1] += a[tmp].sum[1];
	}
	void build(int &root, int l, int r) {
		root = ++size;
		if (l == r) {
			a[root].sum[0] = 1;
			return;
		}
		int mid = (l + r) / 2;
		build(a[root].lc, l, mid);
		build(a[root].rc, mid + 1, r);
		update(root);
	}
	void init(int x) {
		n = x;
		root = size = 0;
		build(root, 1, n);
	}
	int query() {
		return a[root].sum[1];
	}
	void pushdown(int root) {
		if (a[root].tag) {
			int tmp = a[root].lc;
			a[tmp].tag ^= true;
			swap(a[tmp].sum[0], a[tmp].sum[1]);
			tmp = a[root].rc;
			a[tmp].tag ^= true;
			swap(a[tmp].sum[0], a[tmp].sum[1]);
			a[root].tag = false;
		}
	}
	void modify(int root, int l, int r, int ql, int qr) {
		if (l == ql && r == qr) {
			a[root].tag ^= true;
			swap(a[root].sum[0], a[root].sum[1]);
			return;
		}
		pushdown(root);
		int mid = (l + r) / 2;
		if (mid >= ql) modify(a[root].lc, l, mid, ql, min(qr, mid));
		if (mid + 1 <= qr) modify(a[root].rc, mid + 1, r, max(mid + 1, ql), qr);
		update(root);
	}
	void modify(int l, int r) {
		modify(root, 1, n, l, r);
	}
} ST;
int n, m, timer, dfn[MAXN], rit[MAXN], depth[MAXN];
int size[MAXN], son[MAXN], up[MAXN], father[MAXN];
vector <int> a[MAXN];
void modify(int pos) {
	while (pos != 0) {
		ST.modify(dfn[up[pos]], dfn[pos]);
		pos = father[up[pos]];
	}
}
void dfs(int pos, int fa) {
	size[pos] = 1, son[pos] = 0;
	depth[pos] = depth[fa] + 1;
	for (auto x : a[pos]) 
		if (x != fa) {
			dfs(x, pos);
			size[pos] += size[x];
			if (size[x] > size[son[pos]]) son[pos] = x;
		}
}
void efs(int pos, int fa, int from) {
	up[pos] = from;
	father[pos] = fa;
	dfn[pos] = ++timer;
	if (son[pos]) efs(son[pos], pos, from);
	for (auto x : a[pos])
		if (x != son[pos] && x != fa) efs(x, pos, x);
	rit[pos] = timer;
}
int main() {
	read(n);
	for (int i = 1; i <= n - 1; i++) {
		int x, y; read(x), read(y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	dfs(1, 0);
	efs(1, 0, 1);
	ST.init(n);
	read(m);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		modify(x), modify(y);
		writeln(ST.query());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/84922322
今日推荐