Water Tree(树链剖分+dfs时间戳)

Water Tree

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

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

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

51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5

output

00010101

树链剖分模板题

  1 #include<iostream>
  2 #include<cstring>
  3 #include<string>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<algorithm>
  7 #include<vector>
  8 #define maxn 500005
  9 #define lson l,mid,rt<<1
 10 #define rson mid+1,r,rt<<1|1
 11 using namespace std;
 12
 13 int tree[maxn<<3],lazy[maxn<<3];
 14 int n;
 15 int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt;
 16 vector<int>ve[maxn];
 17
 18 void pushup(int rt){
 19     if(tree[rt<<1]||tree[rt<<1|1])
 20         tree[rt]=1;
 21     else
 22         tree[rt]=0;
 23 }
 24
 25 void pushdown(int rt){
 26     if(lazy[rt]!=-1){
 27         lazy[rt<<1]=lazy[rt];
 28         lazy[rt<<1|1]=lazy[rt];
 29         tree[rt<<1]=lazy[rt];
 30         tree[rt<<1|1]=lazy[rt];
 31         lazy[rt]=-1;
 32     }
 33 }
 34
 35 void build(int l,int r,int rt){
 36     lazy[rt]=-1;
 37     if(l==r){
 38         tree[rt]=0;
 39         return;
 40     }
 41     int mid=(l+r)/2;
 42     build(lson);
 43     build(rson);
 44     pushup(rt);
 45 }
 46
 47 void add(int L,int R,int k,int l,int r,int rt){
 48     if(L<=l&&R>=r){
 49         tree[rt]=k;
 50         lazy[rt]=k;
 51         return;
 52     }
 53     int mid=(l+r)/2;
 54     pushdown(rt);
 55     if(L<=mid) add(L,R,k,lson);
 56     if(R>mid) add(L,R,k,rson);
 57     pushup(rt);
 58 }
 59
 60 int query(int L,int R,int l,int r,int rt){
 61     if(L<=l&&R>=r){
 62         return tree[rt];
 63     }
 64     int mid=(l+r)/2;
 65     pushdown(rt);
 66     int ans=0;
 67     if(L<=mid) if(ans||query(L,R,lson)) ans=1;
 68     if(R>mid) if(ans||query(L,R,rson)) ans=1;
 69     pushup(rt);
 70     return ans;
 71 }
 72
 73 void dfs1(int now,int f,int deep){
 74     dep[now]=deep;
 75     siz[now]=1;
 76     fa[now]=f;
 77     int maxson=-1;
 78     for(int i=0;i<ve[now].size();i++){
 79         if(ve[now][i]==f) continue;
 80         dfs1(ve[now][i],now,deep+1);
 81         siz[now]+=siz[ve[now][i]];
 82         if(siz[ve[now][i]]>maxson){
 83             maxson=siz[ve[now][i]];
 84             son[now]=ve[now][i];
 85         }
 86     }
 87 }
 88
 89 void dfs2(int now,int topp){
 90     id[now]=++cnt;
 91     top[now]=topp;
 92     if(!son[now]) return;
 93     dfs2(son[now],topp);
 94     for(int i=0;i<ve[now].size();i++){
 95         if(ve[now][i]==son[now]||ve[now][i]==fa[now]) continue;
 96         dfs2(ve[now][i],ve[now][i]);
 97     }
 98 }
 99
100 void addRange(int x,int y,int k){
101     while(top[x]!=top[y]){
102         if(dep[top[x]]<dep[top[y]]) swap(x,y);
103         add(id[top[x]],id[x],k,1,n,1);
104         x=fa[top[x]];
105     }
106     if(dep[x]>dep[y]) swap(x,y);
107     add(id[x],id[y],k,1,n,1);
108 }
109
110 void addSon(int x,int k){
111     add(id[x],id[x]+siz[x]-1,k,1,n,1);
112 }
113
114 int main(){
115     std::ios::sync_with_stdio(false);
116     cin>>n;
117     int m;
118     int pos,z,x,y;
119     for(int i=1;i<n;i++){
120         cin>>x>>y;
121         ve[x].push_back(y);
122         ve[y].push_back(x);
123     }
124     cnt=0;
125     dfs1(1,0,1);
126     dfs2(1,1);
127     build(1,n,1);
128     cin>>m;
129     for(int i=1;i<=m;i++){
130         cin>>pos>>x;
131         if(pos==1){
132             addSon(x,1);
133         }
134         else if(pos==2){
135             addRange(1,x,0);
136             add(id[x],id[x],0,1,n,1);
137         }
138         else if(pos==3){
139             if(query(id[x],id[x],1,n,1)){
140                 cout<<1<<endl;
141             }
142             else{
143                 cout<<0<<endl;
144             }
145         }
146     }
147
148 }

原文地址:https://www.cnblogs.com/Fighting-sh/p/9704356.html

时间: 2024-11-06 17:29:47

Water Tree(树链剖分+dfs时间戳)的相关文章

codeforces 343D Water Tree 树链剖分 dfs序 线段树 set

题目链接 这道题主要是要考虑到同一棵子树中dfs序是连续的 然后我就直接上树剖了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN=600005; 4 5 struct Node 6 { 7 int l,r; 8 int value; 9 void init() 10 { 11 l=r=value=0; 12 } 13 }tree[4*MAXN]; 14 vector<int>nei[MAXN]

CodeForces 343D 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, t

CF343D Water Tree 树链剖分

问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; template <typename Tp> void read(Tp &x){ x=0;char ch=1;int fh; while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include

poj 3237 Tree 树链剖分+线段树

Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions

spoj Query on a tree(树链剖分模板题)

375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

【CF725G】Messages on a Tree 树链剖分+线段树

[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息传输过程如下: 1.信息的发起者把信息上传给他父亲节点处的跳蚤,然后自身进入等待状态.3.跳蚤国王在收到信息时会将信息立刻下传到发来信息的那个儿子,跳蚤国王可以在同一时刻下传多份信息.4.上传:a把信息传给b.如果b正处于等待状态,则b会立刻将a发来的信息下传回去.如果同时有好多个信息传给b,则b会