Codeforces Round #200 (Div. 1) D. Water Tree (树链剖分)

D. Water Tree

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.

The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.

Mike wants to do the following operations with the tree:

  1. Fill vertex v with water. Then v and all its children are filled with water.
  2. Empty vertex v. Then v and all its ancestors are emptied.
  3. Determine whether vertex v is filled with water at the moment.

Initially all vertices of the tree are empty.

Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers a ib i (1 ≤ a i, b i ≤ na i ≠ b i) — the edges of the tree.

The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers c i (1 ≤ c i ≤ 3), v i (1 ≤ v i ≤ n), where c i is the operation type (according to the numbering given in the statement), and v i is the vertex on which the operation is performed.

It is guaranteed that the given graph is a tree.

Output

For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.

Examples

input

Copy

5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5

output

Copy

0
0
0
1
0
1
0
1

题目大意 :

对于一棵N(5e5)的树,根为1,初始每个点权值为0,有三种操作,一是将以x为根的子树权值全部变成1,二是将x到点1路径上的权值全部变成0,三是输出点x的权值

解法:

树链剖分套上去,剩下就是线段树操作了。

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 5e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

// 存图
vector <int> G[N << 1];
int fz[N], son[N], sz[N];
int idx[N], ev[N], dfn;
int dep[N], top[N];
int n, q;

void dfs1(int x, int fa, int dist) {
	fz[x] = fa;     // 父亲
	dep[x] = dist;  //深度
	sz[x] = 1;      // 子树大小

	int mx = 0;   // 重儿子个数
	for (auto v : G[x]) {
		if (v == fa)
			continue;
		dfs1(v, x, dist + 1);
		sz[x] += sz[v];
		if (sz[v] > mx)
			mx = sz[v], son[x] = v;
	}
}
void dfs2(int x, int topfz) {
	idx[x] = ++dfn;      // 重新编号
	top[x] = topfz;

	if (!son[x])
		return;
	dfs2(son[x], topfz);

	for (auto v : G[x]) {
		if (v != fz[x] && v != son[x])
			dfs2(v, v);
	}
}


// 线段树
int lzy[N * 4], wi[N * 4];  // 标记,权值

void Build(int rt, int l, int r) {
	lzy[rt] = -1;
	if (l == r) 
		return;

	int mid = (l + r) >> 1;
	Build(ls, l, mid);
	Build(rs, mid + 1, r);
}
void Pushdown(int rt, int len) {
	lzy[ls] = lzy[rt];
	lzy[rs] = lzy[rt];

	wi[ls] = lzy[rt] * (len - (len >> 1)); 
	wi[rs] = lzy[rt] * (len >> 1); 

	lzy[rt] = -1;
}
void Update(int rt, int L, int R, int l, int r, int w) {
	if (L >= l && R <= r) {
		wi[rt] = (R - L + 1) * w;   // 赋值
		lzy[rt] = w;
		return;
	}
	if (lzy[rt] != -1)
		Pushdown(rt, R - L + 1);
	int mid = (L + R) >> 1;
	if (mid >= l)
		Update(ls, L, mid, l, r, w);
	if (mid < r)
		Update(rs, mid + 1, R, l, r, w);
	wi[rt] = wi[ls] + wi[rs];
}
void Range_Update(int x, int y, int w) {
	while (top[x] != top[y]) {    // 轻链
		if (dep[top[x]] < dep[top[y]])
			swap(x, y);
		Update(1, 1, n, idx[top[x]], idx[x], w);
		x = fz[top[x]];
	}
	if (dep[x] > dep[y])    // 重链
		swap(x, y);
	Update(1, 1, n, idx[x], idx[y], w);
}
int Query(int rt, int L, int R, int x) {
	if (L == x && R == x)
		return wi[rt];
	if (lzy[rt] != -1)
		Pushdown(rt, R - L + 1);
	int mid = (L + R) >> 1;
	if (mid >= x)
		return Query(ls, L, mid, x);
	else
		return Query(rs, mid + 1, R, x);
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++) {
		int u, v;
		sc("%d %d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs1(1, 0, 1);
	dfs2(1, 1);
	Build(1, 1, n);    // 树链剖分建树

	cin >> q;
	while (q--) {
		int op, x;
		sc("%d %d", &op, &x);
		if (op == 1)               // 子树修改
			Update(1, 1, n, idx[x], idx[x] + sz[x] - 1, 1);
		else if (op == 2)          // 路径修改
			Range_Update(1, x, 0);
		else 
			printf("%d\n", Query(1, 1, n, idx[x]));
	}
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/107056134