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


基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值