首页 > 学院 > 开发设计 > 正文

codeforces 343 D. Water Tree (树链剖分)

2019-11-06 06:17:42
字体:
来源:转载
供稿:网友

D. Water Treetime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 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.

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

题解:树链剖分

小水题,不解释。。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 1000003using namespace std;int n,m,tot,l[N],r[N],point[N],nxt[N],v[N],sz;int fa[N],deep[N],size[N],son[N],pos[N],belong[N],tr[N*4],delta[N*4];void add(int x,int y){	tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;	tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;}void dfs(int x,int f){	size[x]=1; deep[x]=deep[f]+1;	for (int i=point[x];i;i=nxt[i]) {		if (v[i]==f) continue;		fa[v[i]]=x;		dfs(v[i],x);		size[x]+=size[v[i]];		if (size[v[i]]>size[son[x]]) son[x]=v[i];	}}void dfs1(int x,int chain){	belong[x]=chain; pos[x]=++sz; l[x]=r[x]=sz;	if (!son[x]) return ;	dfs1(son[x],chain);	for (int i=point[x];i;i=nxt[i])	 if(v[i]!=fa[x]&&v[i]!=son[x]) dfs1(v[i],v[i]);	r[x]=sz;}void pushdown(int now){	if (delta[now]!=-1) {		delta[now<<1]=delta[now];		delta[now<<1|1]=delta[now];		tr[now<<1]=delta[now]; tr[now<<1|1]=delta[now];		delta[now]=-1;	}}void qjchange(int now,int l,int r,int ll,int rr,int val){	if (ll<=l&&r<=rr) {		tr[now]=val;		delta[now]=val;		return;	}	int mid=(l+r)/2;	pushdown(now);	if (ll<=mid)  qjchange(now<<1,l,mid,ll,rr,val);	if (rr>mid) qjchange(now<<1|1,mid+1,r,ll,rr,val);}int find(int now,int l,int r,int x){	if (l==r) return tr[now];	int mid=(l+r)/2;	pushdown(now);	if (x<=mid) return find(now<<1,l,mid,x);	else return find(now<<1|1,mid+1,r,x);}void solve(int x,int y){	while (belong[x]!=belong[y]){		if (deep[belong[x]]<deep[belong[y]]) swap(x,y);		qjchange(1,1,n,pos[belong[x]],pos[x],0);        x=fa[belong[x]];	}	if (deep[x]>deep[y]) swap(x,y);	qjchange(1,1,n,pos[x],pos[y],0);}int main(){	freopen("a.in","r",stdin);	scanf("%d",&n);	for (int i=1;i<n;i++) {		int x,y; scanf("%d%d",&x,&y);		add(x,y);	}	dfs(1,0); dfs1(1,1);	memset(delta,-1,sizeof(delta));	scanf("%d",&m);	for (int i=1;i<=m;i++) {		int opt,x; scanf("%d%d",&opt,&x);		if (opt==1) qjchange(1,1,n,l[x],r[x],1);		if (opt==2) solve(1,x);		if (opt==3) printf("%d/n",find(1,1,n,pos[x]));	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表