国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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]));	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合江县| 卢湾区| 武鸣县| 大姚县| 嵊泗县| 财经| 治多县| 织金县| 湟源县| 逊克县| 琼海市| 丽江市| 中阳县| 潞西市| 合作市| 马关县| 图们市| 盐城市| 巢湖市| 津市市| 乌苏市| 汉阴县| 紫阳县| 阳城县| 彰武县| 泰和县| 祁东县| 谢通门县| 长乐市| 南和县| 宝鸡市| 平邑县| 洛宁县| 武安市| 民和| 绥棱县| 济南市| 万州区| 湛江市| 玉树县| 于田县|