Water Tree

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

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

0
0
0
1
0
1
0
1

Solution:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

const int N = 500010;

int pz[N], pv[N], fin[N];
int s[N], val[N];
int kw, w[N];
int ss[2 * N], ff[2 * N], pred[2 * N], last[N];

void dfs(int v, int pr) {
w[++kw] = v;
pz[v] = kw;
int j = last[v];
while (j > 0) {
if (ff[j] != pr) {
pv[ff[j]] = v;
dfs(ff[j], v);
}
j = pred[j];
}
fin[v] = kw;
}

int main() {
int n;
scanf("%d", &n);
int m = n - 1;
for (int i = 1; i <= m; i++) {
    scanf("%d %d", ss + i, ff + i);
    ss[i + m] = ff[i];
    ff[i + m] = ss[i];
  }
  for (int i = 1; i <= n; i++) last[i] = 0;
  for (int i = 1; i <= m + m; i++) {
    pred[i] = last[ss[i]];
    last[ss[i]] = i;
  }
  kw = 0;
  dfs(1, 0);
  for (int i = 1; i <= n; i++) s[i] = 0;
  for (int i = 1; i <= n; i++) {
    s[i]++;
    int j = (i | (i - 1)) + 1;
    if (j <= n) s[j] += s[i];
    val[i] = 1;
  }
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    int com, v;
    scanf("%d %d", &com, &v);
    if (com == 1) {
      int sum = 0;
      int x = pz[v] - 1;
      while (x > 0) {
sum += s[x];
x &= x - 1;
}
int cur = 0;
x = fin[v];
while (x > 0) {
cur += s[x];
x &= x - 1;
}
if (cur == sum) continue;
int last = pz[v] - 1;
while (cur > sum) {
int ll = last + 1, rr = fin[v];
while (ll < rr) {
          int mid = (ll + rr) >> 1;
x = mid;
int q = 0;
while (x > 0) {
q += s[x];
x &= x - 1;
}
if (q > sum) rr = mid;
else ll = mid + 1;
}
x = ll;
val[x] = 0;
while (x <= n) {
          s[x]--;
          x = (x | (x - 1)) + 1;
        }
        last = ll;
        cur--;
      }
      if (v != 1) {
        int x = pz[pv[v]];
        if (val[x] == 0) {
          val[x] = 1;
          while (x <= n) {
            s[x]++;
            x = (x | (x - 1)) + 1;
          }
        }
      }
    } else
    if (com == 2) {
      if (val[pz[v]] == 0) {
        int x = pz[v];
        val[x] = 1;
        while (x <= n) {
          s[x]++;
          x = (x | (x - 1)) + 1;
        }
      }
    } else {
      int sum = 0;
      int x = fin[v];
      while (x > 0) {
sum += s[x];
x &= x - 1;
}
x = pz[v] - 1;
while (x > 0) {
sum -= s[x];
x &= x - 1;
}
if (sum > 0) printf("0\n");
else printf("1\n");
}
}
return 0;
}