Codeforces Round #200 (Div. 1) 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:
- 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.
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.
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.
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.
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
0
0
0
1
0
1
0
1
思路:跟bzoj4034类似;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=5e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; ///数组大小 struct edge { int v,next; } edge[N<<1]; int head[N<<1],edg,id,n; /// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数 int ran[N],top[N],tid[N],mx[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid void init() { memset(son,-1,sizeof(son)); memset(head,-1,sizeof(head)); edg=0; id=0; } void add(int u,int v) { edg++; edge[edg].v=v; edge[edg].next=head[u]; head[u]=edg; } void dfs1(int u,int fath,int deep) { fa[u]=fath; siz[u]=1; dep[u]=deep; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(v==fath)continue; dfs1(v,u,deep+1); siz[u]+=siz[v]; if(son[u]==-1||siz[v]>siz[son[u]]) son[u]=v; } } void dfs2(int u,int tp) { tid[u]=mx[u]=++id; top[u]=tp; ran[tid[u]]=u; if(son[u]==-1)return; dfs2(son[u],tp),mx[u]=max(mx[u],mx[son[u]]); for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(v==fa[u])continue; if(v!=son[u]) dfs2(v,v),mx[u]=max(mx[u],mx[v]); } } struct SGT { int sum[N<<2],lazy[N<<2]; void pushup(int pos) { sum[pos]=sum[pos<<1]+sum[pos<<1|1]; } void pushdown(int pos,int l,int r) { if(lazy[pos]!=-1) { int mid=(l+r)>>1; lazy[pos<<1]=lazy[pos]; lazy[pos<<1|1]=lazy[pos]; sum[pos<<1]=lazy[pos]*(mid-l+1); sum[pos<<1|1]=lazy[pos]*(r-mid); lazy[pos]=-1; } } void build(int l,int r,int pos) { lazy[pos]=-1; sum[pos]=0; if(l==r)return; int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos); } void update(int L,int R,int c,int l,int r,int pos) { if(L<=l&&r<=R) { sum[pos]=c*(r-l+1); lazy[pos]=c; return; } pushdown(pos,l,r); int mid=(l+r)>>1; if(L<=mid)update(L,R,c,l,mid,pos<<1); if(R>mid) update(L,R,c,mid+1,r,pos<<1|1); pushup(pos); } int query(int p,int l,int r,int pos) { if(l==r)return sum[pos]; pushdown(pos,l,r); int mid=(l+r)>>1; if(p<=mid)return query(p,l,mid,pos<<1); else return query(p,mid+1,r,pos<<1|1); } }tree; void up(int l,int r) { while(top[l]!=top[r]) { if(dep[top[l]]<dep[top[r]])swap(l,r); tree.update(tid[top[l]],tid[l],0,1,n,1); l=fa[top[l]]; } if(dep[l]<dep[r])swap(l,r); tree.update(tid[r],tid[l],0,1,n,1); } int main() { init(); scanf("%d",&n); for(int i=1; i<n; i++) { int u,v; scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs1(1,-1,1); dfs2(1,1); tree.build(1,n,1); int q; scanf("%d",&q); while(q--) { int t,x; scanf("%d%d",&t,&x); if(t==1) tree.update(tid[x],mx[x],1,1,n,1); else if(t==2) up(1,x); else printf("%d\n",tree.query(tid[x],1,n,1)); } return 0; }