• Water Tree CodeForces


    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
    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


      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<string.h>
      4 #include<algorithm>
      5 using namespace std;
      6 const int N=5e5+10;
      7 int sum[N*4],lazy[N*4];//线段树 
      8 int n,m,r,mod;//节点数,操作数,根节点,模数 
      9 int first[N],tot; //邻接表 
     10 //重儿子,每个节点新编号,父亲,编号,深度,子树个数,所在重链的顶部
     11 int son[N],id[N],fa[N],cnt,deep[N],size[N],top[N];
     12 int w[N],wt[N];// 初始点权,新编号点权
     13 int res=0;//查询答案
     14 
     15 struct edge{
     16     int v,next;
     17 }e[N*4]; 
     18 
     19 void add_edge(int u,int v){
     20     e[tot].v=v;
     21     e[tot].next=first[u];
     22     first[u]=tot++;
     23 }
     24 
     25 void init(){
     26     memset(first,-1,sizeof(first));
     27     tot=0;
     28     cnt=0;
     29 }
     30 
     31 int pushup(int rt){
     32     sum[rt]=(sum[rt*2]&&sum[rt*2+1]);
     33 }
     34 
     35 void pushdown(int rt,int m){
     36     if(lazy[rt]!=-1){
     37         lazy[rt*2]=lazy[rt];
     38         lazy[rt*2+1]=lazy[rt];
     39         sum[rt*2]=lazy[rt];
     40         sum[rt*2+1]=lazy[rt];
     41         lazy[rt]=-1;
     42     }
     43 }
     44 
     45 void build(int l,int r,int v){
     46     lazy[v]=-1;
     47     if(l==r){
     48         sum[v]=0;
     49         return ;
     50     }
     51     int mid=(l+r)/2;
     52     build(l,mid,v*2);
     53     build(mid+1,r,v*2+1);    
     54     pushup(v);
     55 }
     56 
     57 void update(int L,int R,int c,int l,int r,int rt){
     58     if(L<=l&&r<=R){
     59         lazy[rt]=c;
     60         sum[rt]=c;
     61         return;
     62     }
     63     pushdown(rt,r-l+1);
     64     int m=(l+r)/2;
     65     if(L<=m) update(L,R,c,l,m,rt*2);
     66     if(R>m) update(L,R,c,m+1,r,rt*2+1);
     67     pushup(rt);
     68 }
     69 
     70 
     71 void dfs1(int u,int f,int d){
     72     deep[u]=d;
     73     fa[u]=f;
     74     size[u]=1;
     75     int maxson=-1;
     76     for(int i=first[u];~i;i=e[i].next){
     77         int v=e[i].v;
     78         if(v==f) continue;
     79         dfs1(v,u,d+1);
     80         size[u]+=size[v];
     81         if(size[v]>maxson){
     82             son[u]=v;
     83             maxson=size[v];
     84         }
     85     }
     86 }
     87 
     88 void dfs2(int u,int topf){
     89     id[u]=++cnt;
     90     wt[cnt]=w[u];
     91     top[u]=topf;
     92     if(!son[u]) return ;
     93     dfs2(son[u],topf);
     94     for(int i=first[u];~i;i=e[i].next){
     95         int v=e[i].v;
     96         if(v==fa[u]||v==son[u]) continue;
     97         dfs2(v,v);
     98     }
     99 }
    100 
    101 
    102 void updrange(int x,int y,int k){
    103     while(top[x]!=top[y]){
    104         if(deep[top[x]]<deep[top[y]]) swap(x,y);
    105         update(id[top[x]],id[x],0,1,n,1);
    106         x=fa[top[x]];
    107     }
    108     if(deep[x]>deep[y]) swap(x,y);
    109     update(id[x],id[y],0,1,n,1);
    110 }
    111 
    112 void upson(int x,int k){
    113     update(id[x],id[x]+size[x]-1,1,1,n,1);
    114 }
    115 
    116 int query(int L,int R,int l,int r,int rt){
    117     if(L<=l&&r<=R){
    118         return sum[rt];
    119     }
    120     pushdown(rt,r-l+1);
    121     int m=(l+r)/2;
    122     if(L<=m) return query(L,R,l,m,rt*2);
    123     else if(R>m) return query(L,R,m+1,r,rt*2+1);
    124 }
    125 
    126 
    127 
    128 
    129 
    130 
    131 int main(){
    132     int u,v;
    133     scanf("%d",&n);
    134     init();
    135     for(int i=1;i<=n-1;i++){
    136         scanf("%d%d",&u,&v);
    137         add_edge(u,v);
    138         add_edge(v,u);
    139     }
    140     dfs1(1,0,1);
    141     dfs2(1,1);
    142     build(1,n,1);
    143     scanf("%d",&m);
    144     while(m--){
    145         int op,x,y,z;
    146         scanf("%d",&op);
    147         if(op==1){
    148             scanf("%d",&x);
    149             upson(x,1);
    150         }
    151         else if(op==2){
    152             scanf("%d",&x);
    153             updrange(1,x,0);
    154         }
    155         else if(op==3){
    156             scanf("%d",&x);
    157             if(query(id[x],id[x],1,n,1)==0) printf("0
    ");
    158             else printf("1
    ");
    159         }
    160     }
    161 } 

    会树链剖分,这道题就是降维打击了

     
  • 相关阅读:
    哥java学识有大进 干回之前的小学生系统像切菜一样简单 不说了 来代码
    祝贺自己操作系统JAVA项目有进展!!
    四则运算的开发
    四则运算app总结
    对其它组评价的反馈
    第三次spring冲刺2
    阅读《构建之法》第13-17章
    对其他组的评价
    第三次spring冲刺1
    阅读11,12章
  • 原文地址:https://www.cnblogs.com/ellery/p/11588586.html
Copyright © 2020-2023  润新知