Codeforces 343D-Water Tree

来源:互联网 发布:sharepoint是什么软件 编辑:程序博客网 时间:2024/04/29 08:33

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 aibi (1 ≤ ai, bi ≤ nai ≠ bi) — 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 ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi 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
51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5
output
00010101


题意:给定一个树,树上有n个点,每个点是一个蓄水池,初始全为空。首先输入一个n,然后输入n - 1行,每行两个点,代表两点之间有边,然后输入一个m,接下来m行操作,操作有3种:1 a,把a及a的所有子孙注水。2 a,把a及a的所有祖先放水。3 a,询问a点有没有水,有输出1,否则0

解题思路:树链剖分



#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n, q, x, y,tot;int L[500009],R[500009], top[500009];int s[500009], nt[1000009], e[1000009], cnt;int ct[500009], mx[500009], fa[500009], dep[500009];int lazy[500009<<2];void dfs(int k, int f){dep[k] = dep[f] + 1;fa[k] = f, ct[k] = 1, mx[k] = 0;for (int i = s[k]; ~i; i = nt[i]){if (e[i] == f) continue;dfs(e[i], k);ct[k] += ct[e[i]];if (ct[e[i]] > ct[mx[k]]) mx[k] = e[i];}}void Dfs(int k, int t){top[k] = !t ? k : top[fa[k]];L[k] = ++cnt;if (mx[k]) Dfs(mx[k], 1);for (int i = s[k]; ~i; i = nt[i]){if (e[i] == fa[k] || e[i] == mx[k]) continue;Dfs(e[i], 0);}R[k] = cnt;}void build(int k, int l, int r){lazy[k] = -1;if (l == r) return;int mid = (l + r) >> 1;build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r);}void update(int k, int l, int r, int ll, int rr,int val){if (l >= ll&&r <= rr) { lazy[k] = val; return; }int mid = (l + r) >> 1;if (lazy[k]>=0){lazy[k << 1] = lazy[k << 1 | 1]=lazy[k];lazy[k] = -1;}if (ll <= mid) update(k << 1, l, mid, ll, rr, val);if (rr > mid) update(k << 1 | 1, mid + 1, r, ll, rr, val);}int query(int k, int l, int r, int p){if (lazy[k] >= 0||l==r) return max(lazy[k],0);int mid = (l + r) >> 1;if (p <= mid) return query(k << 1, l, mid, p);else return query(k << 1 | 1, mid + 1, r, p);}void solve(int x, int y){while (top[x] != top[y]){if (dep[top[x]] < dep[top[y]]) swap(x, y);update(1, 1, n, L[top[x]], L[x], 0); x = fa[top[x]];}if (dep[x] > dep[y]) swap(x, y);update(1, 1, n, L[x], L[y], 0);}int main(){//freopen("input.txt", "r", stdin);//freopen("output.txt", "w", stdout);while (~scanf("%d", &n)){memset(s, -1, sizeof s);dep[0] = ct[0] = cnt = 0;for (int i = 1; i < n; i++){scanf("%d%d", &x, &y);nt[cnt] = s[x], s[x] = cnt, e[cnt++] = y;nt[cnt] = s[y], s[y] = cnt, e[cnt++] = x;}dfs(1, 0);Dfs(1, cnt= 0);build(1, 1, n);scanf("%d", &q);while (q--){scanf("%d%d", &x,&y);if (x == 1) update(1, 1, n, L[y], R[y], 1);else if (x == 2) solve(1, y);else printf("%d\n", query(1, 1, n, L[y]));}}return 0;}

原创粉丝点击