zoukankan      html  css  js  c++  java
  • CoderForces343D:Water Tree(dfs序+线段树&&特殊处理)

    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.

    Example

    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

    题意:给定一棵树,没棵树上有一个水池,现在有如下操作,

              1,给V和其子树装水。

              2,对V和其祖先排水。

              3,问V是否有水。

    思路1:看到操作1,操作子树,很自然地想到dfs序;然后但是2操作,V及其祖先在线段树上的位置是离散的,所以需要暴力。

               转化一下思路:本来是多点更改2,单点查询3; 改为:单点更新2,区间查询3。

               即:3操作改为,查询V及其子树是否都有水。

    注意:对1操作,假设V的子树有空(存在无水的点)的,则需要把V的父亲改为空。     因为V的子树有空,则V的父亲为空,而且给V和子树加水后V父亲任然为空。但是由于没有单点更改V的父亲,而V的父亲和其子树都满水,则导致误判以为V的父亲有水。  而V的父亲最具有代表性,只改V的父亲节点即可。

    路2:2操作用树链剖分,避免了暴力修改,效率还过得去。

    (下面的思路1的代码)

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=500010;
    int Laxt[maxn],Next[maxn<<1],To[maxn<<1],cnt;
    int fa[maxn],in[maxn],out[maxn],times;
    void add(int u,int v)
    {
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
    }
    void dfs(int u,int pre)
    {
        fa[u]=pre; in[u]=++times;
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i];
            if(v!=pre) dfs(v,u);
        } out[u]=times;
    }
    int Lazy[maxn<<2],all[maxn<<2];
    struct DFSTree{
        void pushdown(int Now)
        {
            if(Lazy[Now]){
                Lazy[Now]=0;
                all[Now<<1]=1; all[Now<<1|1]=1;
                Lazy[Now<<1]=1; Lazy[Now<<1|1]=1; 
            }
        }
        void pushup(int Now)
        {
            all[Now]=all[Now<<1]&all[Now<<1|1];
        }
        void add(int Now,int L,int R,int l,int r)
        {
        
            if(l<=L&&r>=R) {
                Lazy[Now]=1; all[Now]=1; return ;
            }
            int Mid=(L+R)>>1;
            pushdown(Now);
            if(l<=Mid) add(Now<<1,L,Mid,l,r);
            if(r>Mid) add(Now<<1|1,Mid+1,R,l,r);
            pushup(Now);
        }
        int query(int Now,int L,int R,int l,int r)
        {    
            if(l<=L&&r>=R) return all[Now];
            int Mid=(L+R)>>1;
            pushdown(Now);
            if(l<=Mid){ if(!query(Now<<1,L,Mid,l,r)) return 0;}
            if(r>Mid) { if(!query(Now<<1|1,Mid+1,R,l,r)) return 0;}
            pushup(Now);
            return 1;
        }
        void del(int Now,int L,int R,int l,int r)
        {
            if(l<=L&&r>=R) {
                Lazy[Now]=0; all[Now]=0; return ;
            }
            int Mid=(L+R)>>1;
            pushdown(Now);
            if(l<=Mid) del(Now<<1,L,Mid,l,r);
            if(r>Mid) del(Now<<1|1,Mid+1,R,l,r);
            /*else{
                del(Now<<1,L,Mid,l,r);
                del(Now<<1|1,Mid+1,R,l,r);
            } */
            pushup(Now);
        }
    }Tree;
    int main()
    {
        int N,Q,u,v,i,j,opt;
        scanf("%d",&N);
        for(i=1;i<N;i++){
            scanf("%d%d",&u,&v);
            add(u,v); add(v,u);
        }
        dfs(1,1);
        scanf("%d",&Q);
        while(Q--){
            scanf("%d%d",&opt,&u);
            if(opt==1){ 
                 if(!Tree.query(1,1,N,in[u],out[u])) 
                    Tree.del(1,1,N,in[fa[u]],in[fa[u]]);
                 Tree.add(1,1,N,in[u],out[u]);
            }
            else if(opt==2) Tree.del(1,1,N,in[u],in[u]);
            else printf("%d
    ",Tree.query(1,1,N,in[u],out[u]));
        }
        return 0;
    }

     加了输入优化后排第二了。

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=500010;
    int Laxt[maxn],Next[maxn<<1],To[maxn<<1],cnt;
    int fa[maxn],in[maxn],out[maxn],times;
    void read(int &res){
        char c=getchar();
        for(;c>'9'||c<'0';c=getchar());
        for(res=0;c>='0'&&c<='9';c=getchar()) res=(res<<3)+(res<<1)+c-'0';
    }
    void add(int u,int v)
    {
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
    }
    void dfs(int u,int pre)
    {
        fa[u]=pre; in[u]=++times;
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i];
            if(v!=pre) dfs(v,u);
        } out[u]=times;
    }
    int Lazy[maxn<<2],all[maxn<<2];
    struct DFSTree{
        void pushdown(int Now)
        {
            if(Lazy[Now]){
                Lazy[Now]=0;
                all[Now<<1]=1; all[Now<<1|1]=1;
                Lazy[Now<<1]=1; Lazy[Now<<1|1]=1; 
            }
        }
        void pushup(int Now)
        {
            all[Now]=all[Now<<1]&all[Now<<1|1];
        }
        void add(int Now,int L,int R,int l,int r)
        {
        
            if(l<=L&&r>=R) {
                Lazy[Now]=1; all[Now]=1; return ;
            }
            int Mid=(L+R)>>1;
            pushdown(Now);
            if(l<=Mid) add(Now<<1,L,Mid,l,r);
            if(r>Mid) add(Now<<1|1,Mid+1,R,l,r);
            pushup(Now);
        }
        int query(int Now,int L,int R,int l,int r)
        {    
            if(l<=L&&r>=R) return all[Now];
            int Mid=(L+R)>>1;
            pushdown(Now);
            if(l<=Mid) if(!query(Now<<1,L,Mid,l,r)) return 0;
            if(r>Mid)  if(!query(Now<<1|1,Mid+1,R,l,r)) return 0;
            pushup(Now);
            return 1;
        }
        void del(int Now,int L,int R,int l,int r)
        {
            if(l<=L&&r>=R) {
                Lazy[Now]=0; all[Now]=0; return ;
            }
            int Mid=(L+R)>>1;
            pushdown(Now);
            if(l<=Mid) del(Now<<1,L,Mid,l,r);
            if(r>Mid) del(Now<<1|1,Mid+1,R,l,r);
            pushup(Now);
        }
    }Tree;
    int main()
    {
        int N,Q,u,v,i,j,opt;
        read(N);
        for(i=1;i<N;i++){
            read(u); read(v);
            add(u,v); add(v,u);
        }
        dfs(1,1);
        read(Q);
        while(Q--){
            read(opt); read(u);
            if(opt==1){ 
                 if(!Tree.query(1,1,N,in[u],out[u])) 
                    Tree.del(1,1,N,in[fa[u]],in[fa[u]]);
                 Tree.add(1,1,N,in[u],out[u]);
            }
            else if(opt==2) Tree.del(1,1,N,in[u],in[u]);
            else printf("%d
    ",Tree.query(1,1,N,in[u],out[u]));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    vue前端使用JsonViewer进行json展示
    vue代理服务器proxy配置
    'vue-cli-service' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
    Python中的高阶函数和内置高阶函数(abs,map,reduce,sorted,filter)
    Ant Design Vue 通过v-decorator实现数据绑定
    Vue脚手架(vue-cli)搭建和目录结构详解
    如何使用Postman从XML提取变量
    【已解决】Vue格式化js自动加上冒号和分号
    vue.js安装与搭建
    Python函数中如何返回多个值?
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8547397.html
Copyright © 2011-2022 走看看