CodeForces - 343D Water Tree

题目链接:http://codeforces.com/problemset/problem/343/D

题意:

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


给出一棵树

完成3种操作:

1.修改一个点及其所有的祖先的值

2.修改以点u为根子树的所有结点的值

3.查询某节点的值


sol:树链剖分,维护dfs序,在开始访问子树和结束访问子树时打标记,树链上一共2*n个结点。

code:

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<sstream>
#include<vector>
#include<queue>
#include<cstdio>

using namespace std;

const int maxn = 1000005;
typedef long long ll;

#define lson rt<<1
#define rson rt<<1|1

int son[maxn<<2];
int Size[maxn<<2];
int f[maxn];
int d[maxn];
int top[maxn];
int dis[maxn];
int in[maxn];
int out[maxn];
int pos[maxn];
int id[maxn];
int dfc;

vector<int> G[maxn];

struct node{
	int l,r;
	int w;
	int lazy;
};

node tree[maxn<<2];

void dfs1(int u,int fa,int step){
//	cout<<"dfs1 "<<u<<' '<<fa<<' '<<step<<endl;
	d[u]=step;
	f[u]=fa;
	Size[u]=1;
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(v==fa) continue;
		dfs1(v,u,step+1);
		Size[u]+=Size[v];
		if(son[u]==-1||Size[v]>Size[son[u]]){
			son[u]=v;
		}
	}
}

void dfs2(int u,int tp){
//	cout<<"dfs2 "<<u<<' '<<tp<<endl;
	top[u] = tp;
	in[u]=++dfc;
	pos[dfc]=u;
	if(son[u]!=-1) {
		dfs2(son[u],tp);
	}
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(v!=son[u]&&v!=f[u]){
			dfs2(v,v);
		}
	}
	out[u]=++dfc;
	pos[dfc]=-u;
}

int Lca(int u,int v){
	while(top[u]!=top[v]){
		if(d[top[u]]<d[top[v]]) swap(u,v);
		u=f[top[u]];
	}
	return d[u]<d[v]?u:v;
}

void Pushup(int rt){
	tree[rt].w=tree[lson].w&&tree[rson].w ? 1:0;
}

void Pushdown(int rt){
	int l=tree[rt].l;
	int r=tree[rt].r;
	int m=(l+r)>>1;
	if(tree[rt].lazy!=-1){
		tree[rson].w = tree[lson].w=tree[rt].lazy;
		tree[lson].lazy=tree[rson].lazy=tree[rt].lazy;
		tree[rt].lazy = -1;
	}
}

void build(int l,int r,int rt){
	tree[rt].l=l;
	tree[rt].r=r;
	tree[rt].w=0;
	tree[rt].lazy=-1;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(l,m,lson);
	build(m+1,r,rson);
	Pushup(rt);
}

void update(int rt,int L,int R,int p){
	int l=tree[rt].l;
	int r=tree[rt].r;
//	cout<<"updaet "<<rt<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<endl;
	if(L<=l&&r<=R){
		tree[rt].w=p;
		tree[rt].lazy=p;
		return ;
	}
	Pushdown(rt);
	int m=(l+r)>>1;
	if(L<=m) update(lson,L,R,p);
	if(m<R) update(rson,L,R,p);
	Pushup(rt);
}

int query(int rt,int u){
	int l=tree[rt].l;
	int r=tree[rt].r;
//	cout<<"query  "<<rt<<' '<<l<<' '<<r<<' '<<u<<endl;
	if(l==r) return tree[rt].w;
	Pushdown(rt);
	int m=(l+r)>>1;
	if(u<=m) return query(lson,u);
	else return query(rson,u);
}

void init(){
	memset(f,-1,sizeof(f));
	memset(son,-1,sizeof(son));
}

void display(){
	for(int i=1;i<=5;i++){
		cout<<i<<' '<<query(1,in[i])<<endl;
	}
	cout<<"-------\n";
}

int main(){
	init();
	int n;
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs1(1,0,0);
	dfs2(1,1);
	build(1,dfc,1);
	int q;
	scanf("%d",&q);
	while(q--){
		int c,v;
		scanf("%d%d",&c,&v);
//		cout<<"cv= "<<c<<' '<<v<<endl;
		if(c==1){
			update(1,in[v],out[v],1);
		}else if(c==2){
			while(top[1]!=top[v]){
				update(1,in[top[v]],in[v],0);
				v=f[top[v]];
			}
			update(1,1,in[v],0);
		}else if(c==3){
			int ans=query(1,in[v]);
			printf("%d\n",ans);
		}
//		display();
	}
	return 0;
}
















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值