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記得改宏定義!!!
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章