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

D. Water Tree
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 - 1lines 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.

Sample test(s)
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

按dfs序建树。

操作2:需要改变点以及它的所有祖先,我们反一转来想,如果一个点的子孙中有一个为0,那么他肯定为0,所以这个只需要单点更新,存区间最小值。

操作3:单点查询。

操作1:如果当前点不是根结点,并且状态为空,那就要注意此时虽然我们没有在线段树上表现出来,但他的祖先们其实状态都是0,所以我们需要把他的父亲先置为0,剩下的就按dfs序区间进行更新。

/*
 *=====================
 *File Name:a.cpp
 *Author: qqspeed
 *Date: 2014年 07月 17日 星期四 14:58:11 CST
 *=====================
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
#define max(a,b) (a<b?b:a)
//#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))

inline int input(){
	int ret=0;bool isN=0;char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') isN=1;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		ret=ret*10+c-'0';
		c=getchar();
	}
	return isN?-ret:ret;
}

inline void output(int x){    
    if(x<0){    
        putchar('-');x=-x;    
    }    
    int len=0,data[11];    
    while(x){    
        data[len++]=x%10;x/=10;    
    }    
    if(!len)    data[len++]=0;    
    while(len--)   
        putchar(data[len]+48);    
    putchar('\n');  
}  


const int MAXN=500005;
int to[MAXN],last[MAXN],cnt;
int n,x,y,status[MAXN<<2],q,fa[MAXN],lazy[MAXN<<2];
struct EDGE{
	int v,next;
}edge[MAXN<<1];
int head[MAXN],e;

inline void addEdge(int u,int v){
	edge[e].v=v;
	edge[e].next=head[u];
	head[u]=e++;
}

inline void dfs(int now,int pre){
	to[now]=(++cnt);
	fa[now]=pre;
	ree(i,now){
		int v=edge[i].v;
		if(v!=pre) dfs(v,now);
	}
	last[now]=cnt;
}

inline void push_up(int t){
	status[t]=min(status[L],status[R]);	
}

inline void push_down(int t){
	if(lazy[t]!=-1){
		status[L]=status[R]=lazy[t];
		lazy[L]=lazy[R]=lazy[t];
		lazy[t]=-1;
	}
}

inline void add(int t,int l,int r,int x,int y,int st){
	if(l>=x && r<=y){
		status[t]=lazy[t]=st;
		return;
	}
	push_down(t);
	MID;
	if(y<=mid) add(L,l,mid,x,y,st);
	else if(x>mid) add(R,mid+1,r,x,y,st);
	else{
		add(L,l,mid,x,mid,st);
		add(R,mid+1,r,mid+1,y,st);
	}
	push_up(t);
}

inline void add(int t,int l,int r,int x,int st){
	if(l==r) {status[t]=st;return;}
	push_down(t);
	MID;
	if(x<=mid) add(L,l,mid,x,st);
	else add(R,mid+1,r,x,st);
	push_up(t);
}

inline int query(int t,int l,int r,int x,int y){
	if(l>=x && r<=y) return status[t];
	push_down(t);
	MID;
	if(y<=mid) return query(L,l,mid,x,y);
	else if(x>mid)return query(R,mid+1,r,x,y);
	else{
		return min(query(L,l,mid,x,mid),query(R,mid+1,r,mid+1,y));
	}
}

void Print(int t){
	if(t<=5){
		printf("%d %d\n",t,status[t]);
		Print(L),Print(R);
	}
}

int main(){
	n=input();clr(head,-1);e=cnt=0;
	rep(i,1,n){
		x=input(),y=input();
		addEdge(x,y);addEdge(y,x);
	}
	dfs(1,0);
	clr(status,0);
	clr(lazy,-1);
	q=input();
	while(q--){
		y=input();
		x=input();
		if(y==1){
			int v=query(1,1,cnt,to[x],last[x]);
			add(1,1,cnt,to[x],last[x],1);
			if(fa[x]!=0 && v==0){
				add(1,1,cnt,to[fa[x]],0);
			}
		}
		else if(y==2){
			add(1,1,cnt,to[x],0);
		}
		else{
			output(query(1,1,cnt,to[x],last[x]));
		}
		//Print(1);puts("");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值