Codeforces Round #200 (Div. 1)D. Water Tree dfs序

D. Water Tree

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/343/problem/D

Description

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.

Bi​​,Ci​​,即此题的初始分值、每分钟减少的分值、dxy做这道题需要花费的时间。

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

Sample Output

0
0
0
1
0
1
0
1

HINT

题意

给你一棵树,初始点权都是0,然后三个操作

1.将一个点以及他的儿子全部变成1

2.将一个点以及他的所有祖先全部变成0

3.查询一个点的权值

题解:

dfs序,然后两棵线段树维护就好了

注意先后顺序就行,如果你变成1的操作先于变成0的操作,那么肯定就1优先咯

反之亦然

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
#define maxn 1500050
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
vector<int> E[maxn];
int id;
int in[maxn];
int out[maxn];
void dfs(int x,int pre)
{
    in[x]=++id;
    for(int i=0;i<E[x].size();i++)
    {
        if(E[x][i]==pre)continue;
        dfs(E[x][i],x);
    }
    out[x]=id;
}
struct Segtree
{
    int sum[maxn<<2];
    int lazy[maxn<<2];

    void pushup(int rt)
    {
        sum[rt] = max(sum[rt<<1], sum[rt<<1|1]);
    }

    void pushdown(int rt, int x)
    {
        if(lazy[rt] != -1) {
            lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
            sum[rt<<1] = lazy[rt];///!!!
            sum[rt<<1|1] = lazy[rt];///!!!
            lazy[rt] = -1;
        }
    }

    void creat(int l, int r, int rt)
    {
        lazy[rt] = -1, sum[rt] = 1;
        if(l == r) return;
        int mid = (l+r)>>1;
        creat(l, mid, rt<<1);
        creat(mid+1, r, rt<<1|1);
        pushup(rt);
    }

    void modify(int l, int r, int x, int L, int R, int rt)
    {
        if(l <= L && r >= R) {
            lazy[rt] = x;
            sum[rt] = x;///!!!
            return;
        }
        pushdown(rt, R-L+1);///!!!
        int mid = (L+R)>>1;
        if(l <= mid) modify(l, r, x, L, mid, rt<<1);
        if(r > mid) modify(l, r, x, mid+1, R, rt<<1|1);
        pushup(rt);
    }

    int query(int l, int r, int x, int L, int R, int rt)
    {
        if(l <= L && r >= R) {
            return sum[rt];
        }
        pushdown(rt, R-L+1);///!!!
        int mid = (L+R)>>1;
        int sum1 = 0,sum2 = 0;
        if(l <= mid) sum1 = query(l, r, x, L, mid, rt<<1);
        if(r > mid) sum2 = query(l, r, x, mid+1, R, rt<<1|1);
        pushup(rt);
        return max(sum1,sum2);
    }
}seg1,seg2;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        E[a].push_back(b);
        E[b].push_back(a);
    }
    dfs(1,-1);
    seg1.creat(1,id,1);
    seg2.creat(1,id,1);
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if(op==1)
            seg1.modify(in[x],out[x],i+1,1,id,1);
        if(op==2)
            seg2.modify(in[x],in[x],i+1,1,id,1);
        if(op==3)
        {
            int tmp1 = seg1.query(in[x],in[x],i,1,id,1);
            int tmp2 = seg2.query(in[x],out[x],i,1,id,1);
            if(tmp1>tmp2)printf("1
");
            else printf("0
");
        }
    }
}
原文地址:https://www.cnblogs.com/qscqesze/p/4872060.html