343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构

Posted Meek

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构相关的知识,希望对你有一定的参考价值。

D. Water Tree
 

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

题意:

给你一棵树,n个点n-1条边,m个询问

(1)“1 v",表示将以点v为根节点的子树全部赋值为1,

(2)"2 v",表示将点v以及点v的所有祖先节点全部赋值为0,

(3)"3 v",表示查询点v的值。

题解:

我们dfs出dfs序列,再用线段树/树状数组修改一段序列就好了(只有01)

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxn = 5e5+100, Pow = 1 << 19;
int N, Q, ind[maxn], r[maxn], cur = 1, a, b;
int emp[2*Pow+5] , fil[2*Pow+5];
vector<int> adj[maxn];
void dfs(int v, int par){
    ind[v] = cur++;
    for(int i = 0; i<adj[v].size(); i++) if(adj[v][i]!=par) dfs(adj[v][i], v);
    r[v] = cur-1;
}
void upd(int l, int r, int t){
    l+=Pow; r+=Pow;
    while(l<=r){
        fil[l] = fil[r] = t;
        l = (l+1)/2;
        r = (r-1)/2;
    }
}
int get(int x){
    int res = 0;
    for(int i = Pow+x; i>0; i/=2) res = max(res, fil[i]);
    return res;
}
void add(int x, int t){
    for(int i = Pow+x-1; i>0; i/=2) emp[i] = t;
}
int rmq(int p, int l, int r, int a, int b){
    if(l>b||r<a) return 0;
    if(l>=a&&r<=b) return emp[p]; 
    return max(rmq(2*p, l, (l+r)/2, a, b), rmq(2*p+1, (l+r)/2+1, r, a, b));
}
int main(){
    ios::sync_with_stdio(false); cin.tie(false); cout.tie(false);
    cin >> N;
    for(int i = 0; i<N-1; i++){
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0, -1);
    cin >> Q;
    for(int i = 1; i<=Q; i++){
        cin >> a >> b;
        b--;
        if(a==1) upd(ind[b], r[b], i);
        else if(a==2) add(ind[b], i);
        else cout << (get(ind[b])>rmq(1, 1, Pow, ind[b], r[b])) << \n;
    }
}

 

 

以上是关于343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #343 (Div. 2) B. Far Relative’s Problem

Codeforces Round #343 (Div. 2) A. Far Relative’s Birthday Cake

Codeforces Round #343 (Div. 2) (C. Famil Door and Brackets(DP))

Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+离散化优化DP)

CF Global Round 20

Javascript Math.ceil()与Math.round()与Math.floor()区别