[Codeforces 343D] Water Tree dfs 트리 + 세그먼트 트리

9543 단어 DFScodeforces
D. Water Tree time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard 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: Fill vertex v with water. Then v and all its children are filled with water. Empty vertex v. Then v and all its ancestors are emptied. 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. sample 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점과 v를 뿌리로 가는 경로에서 물을 빼기 3, v점에 물이 있는지 묻기
사고방식: 1.ffs 트리, 처리 가능한 구간;2. 라인 트리 관개(구간 갱신)+lazy 처리;3. 워터프루프 갱신하기;
주의:1.물 조건(push-up);2. 관개 구간에 시간이 있으면---아버지 노드는 반드시 비어있다(판단+단점갱신)!!!
부호를 붙이다
#include<iostream>
#include<vector>
#include<stdio.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
int n;
int in[550000];
int out[550000];
int tree[550000*8];
int pos[550000];
int lazy[550000*8];
int num=0;
vector<int> lin[550000];
bool flag=false;
void dfs(int x,int pre)
{
    in[x]=++num;
    for(int i=0;i<lin[x].size();i++)
    {
        int v=lin[x][i];
        if(v!=pre)
        {
            pos[v]=x;
            dfs(v,x);
        }
    }
    out[x]=num;
}
void push_down(int id,int l,int mid,int r)
{
    if(lazy[id]==0) return ;
    tree[lson]=tree[rson]=lazy[lson]=lazy[rson]=1;
    lazy[id]=0;
}
void push_up(int id)
{
    if(tree[lson]+tree[rson]==2) tree[id]=1;
    else tree[id]=0;
}
void add(int id,int l,int r,int L,int R,int V)
{
    if(l==0) return ;
    if(l>R||r<L) return ;
    if(l>=L&&r<=R)
    {
        if(tree[id]==0) flag=1;
        tree[id]=V;
        if(V==1) lazy[id]=V;
        return ;
    }
    int mid=(l+r)/2;
    push_down(id,l,mid,r);
    add(lson,l,mid,L,R,V);
    add(rson,mid+1,r,L,R,V);
    push_up(id);
}
void query(int id,int l,int r,int L,int R)
{
    if(l>R||r<L) return ;
    if(l>=L&&r<=R)
    {
        if(tree[id]==0) flag=1;
        return ;
    }
    int mid=(l+r)>>1;
    push_down(id,l,mid,l);
    query(lson,l,mid,L,R);
    query(rson,mid+1,r,L,R);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int aa,bb;
        scanf("%d%d",&aa,&bb);
        lin[aa].push_back(bb);
        lin[bb].push_back(aa);
    }
    pos[1]=0;
    dfs(1,0);
    int Q;
    scanf("%d",&Q);
    for(int i=1;i<=Q;i++)
    {
        int aa,bb;
        scanf("%d%d",&aa,&bb);
        if(aa==1)
        {
            flag=false;//2.          ————     !
            add(1,1,n,in[bb],out[bb],1);
            if(flag==true)
            {
                add(1,1,n,in[pos[bb]],in[pos[bb]],0);
            }
        }
        else
        if(aa==2)
        {
            add(1,1,n,in[bb],in[bb],0);
        }
        if(aa==3)
        {
            flag=false;
            query(1,1,n,in[bb],out[bb]);
            if(flag==true) printf("0
"
); else printf("1
"
); } } }

좋은 웹페이지 즐겨찾기