天天看點

codeforces contest 343 problem D(線段樹+dfs序)

 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 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ 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

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      

題意:兩種操作,将節點u置1 它的子樹為1 ,将u置0 它的祖先全為0;

解:這題操作太複雜,是以需要轉換思維,如果将一個節點置1 先查詢它的子樹節點是否有0 如果有就将它的父親置0  然後直接将它的子樹區間置1,如果将一個節點置0 将這個節點的左區間端點置0 這樣查詢時 查詢這個節點的子樹中 是否有為0的節點就可以了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6+10;
typedef long long LL;
vector<int>p[N];
struct node
{
    int l, r, v, f;
}g[N];
int cnt;
void dfs(int u,int f)
{
    g[u].l=++cnt,g[u].f=f;
    for(int i=0;i<p[u].size();i++)
    {
        int v=p[u][i];
        if(v==f) continue;
        dfs(v,u);
    }
    g[u].r=++cnt;
    return ;
}
int sum[N<<1];
void build(int l,int r,int rt)
{
    sum[rt]=0;
    if(l==r) return ;
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    return ;
}
void pushup(int rt)
{
    sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);
    return ;
}
void pushdown(int rt)
{
    if(sum[rt]==1) sum[rt<<1]=sum[rt<<1|1]=1;
    return ;
}
void del(int x,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=0;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)/2;
    if(x<=mid) del(x,l,mid,rt<<1);
    else del(x,mid+1,r,rt<<1|1);
    pushup(rt);
    return ;
}

int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)  return sum[rt];
    pushdown(rt);
    int mid=(l+r)/2,x=1;
    if(L<=mid) x=min(x,query(L,R,l,mid,rt<<1));
    if(R>mid) x=min(x,query(L,R,mid+1,r,rt<<1|1));
    pushup(rt);
    return x;
}
void update(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        sum[rt]=1;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)/2,x=1;
    if(L<=mid) update(L,R,l,mid,rt<<1);
    if(R>mid) update(L,R,mid+1,r,rt<<1|1);
    pushup(rt);
    return ;
}

int main()
{
    int n, q, x, y;
    scanf("%d", &n);
    for(int i=1;i<n;i++)
    {

        scanf("%d %d", &x, &y);
        p[x].push_back(y),p[y].push_back(x);
    }
    cnt=0;
    dfs(1,-1);
    build(1,cnt,1);
    scanf("%d", &q);
    while(q--)
    {
        scanf("%d %d", &x, &y);
        if(x==1)
        {
            if(g[y].f!=-1&&query(g[y].l,g[y].r,1,cnt,1)==0) del(g[g[y].f].l,1,cnt,1);
            update(g[y].l,g[y].r,1,cnt,1);
        }
        else if(x==2)   del(g[y].l,1,cnt,1);
        else  printf("%d\n",query(g[y].l,g[y].r,1,cnt,1));
    }
    return 0;
}
           

繼續閱讀