D. Water Tree

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/343/problem/D

Description

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 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ 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.

B​i​​,C​i​​,即此题的初始分值、每分钟减少的分值、dxy做这道题需要花费的时间。

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

Sample Output

0
0
0
1
0
1
0
1

HINT

题意

给你一棵树,初始点权都是0,然后三个操作

1.将一个点以及他的儿子全部变成1

2.将一个点以及他的所有祖先全部变成0

3.查询一个点的权值

题解:

dfs序,然后两棵线段树维护就好了

注意先后顺序就行,如果你变成1的操作先于变成0的操作,那么肯定就1优先咯

反之亦然

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
#define maxn 1500050
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
vector<int> E[maxn];
int id;
int in[maxn];
int out[maxn];
void dfs(int x,int pre)
{
in[x]=++id;
for(int i=;i<E[x].size();i++)
{
if(E[x][i]==pre)continue;
dfs(E[x][i],x);
}
out[x]=id;
}
struct Segtree
{
int sum[maxn<<];
int lazy[maxn<<]; void pushup(int rt)
{
sum[rt] = max(sum[rt<<], sum[rt<<|]);
} void pushdown(int rt, int x)
{
if(lazy[rt] != -) {
lazy[rt<<] = lazy[rt<<|] = lazy[rt];
sum[rt<<] = lazy[rt];///!!!
sum[rt<<|] = lazy[rt];///!!!
lazy[rt] = -;
}
} void creat(int l, int r, int rt)
{
lazy[rt] = -, sum[rt] = ;
if(l == r) return;
int mid = (l+r)>>;
creat(l, mid, rt<<);
creat(mid+, r, rt<<|);
pushup(rt);
} void modify(int l, int r, int x, int L, int R, int rt)
{
if(l <= L && r >= R) {
lazy[rt] = x;
sum[rt] = x;///!!!
return;
}
pushdown(rt, R-L+);///!!!
int mid = (L+R)>>;
if(l <= mid) modify(l, r, x, L, mid, rt<<);
if(r > mid) modify(l, r, x, mid+, R, rt<<|);
pushup(rt);
} int query(int l, int r, int x, int L, int R, int rt)
{
if(l <= L && r >= R) {
return sum[rt];
}
pushdown(rt, R-L+);///!!!
int mid = (L+R)>>;
int sum1 = ,sum2 = ;
if(l <= mid) sum1 = query(l, r, x, L, mid, rt<<);
if(r > mid) sum2 = query(l, r, x, mid+, R, rt<<|);
pushup(rt);
return max(sum1,sum2);
}
}seg1,seg2;
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
}
dfs(,-);
seg1.creat(,id,);
seg2.creat(,id,);
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int op,x;
scanf("%d%d",&op,&x);
if(op==)
seg1.modify(in[x],out[x],i+,,id,);
if(op==)
seg2.modify(in[x],in[x],i+,,id,);
if(op==)
{
int tmp1 = seg1.query(in[x],in[x],i,,id,);
int tmp2 = seg2.query(in[x],out[x],i,,id,);
if(tmp1>tmp2)printf("1\n");
else printf("0\n");
}
}
}

Codeforces Round #200 (Div. 1)D. Water Tree dfs序的更多相关文章

  1. 343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构

    D. Water Tree   Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...

  2. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  3. 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 ...

  4. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

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

    简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...

  7. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  8. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)

    https://codeforces.com/contest/1062/problem/E 题意 给一颗树n,然后q个询问,询问编号l~r的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...

随机推荐

  1. PHP超大文件下载,断点续传下载

    源代码: <?php $sourceFile = "1.tmp"; //要下载的临时文件名 $outFile = "用户订单.xls"; //下载保存到客 ...

  2. 将archlinux 2013-06-01版,安装配置为个人工作站

    本文安装所使用的镜像为:archlinux-2013.06.01-dual.iso.首先请看看我安装完成之后的效果.图一,是第一个虚拟桌面及右键菜单图: 图二,是第二个虚拟桌面效果图.后几个虚拟桌面图 ...

  3. [转] Splay Tree(伸展树)

    好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...

  4. Java [leetcode 18]4Sum

    问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...

  5. SharePoint 2010 Pop-Up Dialogs

    转:http://kyleschaeffer.com/sharepoint/sharepoint-2010-pop-up-dialogs/ SharePoint 2010 makes it incre ...

  6. 从linux启动到rootfs的挂载分析

    简单的来说,根文件系统包括虚拟根文件系统和真实根文件系统.在Kernel启动的初始阶段,首先去创建虚拟的根文件系统,接下来再去调用do_mount来加载真正的文件系统,并将根文件系统切换到真正的文件系 ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.3

    Let $\scrM$ be a $p$-dimensional subspace of $\scrH$ and $\scrN$ its orthogonal complement. Choosing ...

  8. 单点登录技术:微软Passport单点登录协议和自由联盟规范

    随着互联网络应用的普及,越来越多的人开始使用互联网上提供的服务.然而目前提供服务的网站大多采用用户名.口令的方式来识别用户身份,这使得用户需要经常性的输入自己的用户名.口令.显然这种认证方式存在着弊端 ...

  9. umask设置导致程序权限不足的问题

    这几天邮件告警总是发不了邮件,查看了下zext_msmtp.sh的日志,发现总是提示权限不足…… 于是切换为zabbix的账户,发现在msmtp的目录下连ls都无法执行. 之后发现是umask的问题, ...

  10. Zabbix探索:模板中发现规则的使用

    其实模板的建立只要多看看系统自带的模板内容就清楚了,一目了然,不用做过多解释. 目前使用到的自动发现规则有端口和文件系统的,其他还没有仔细研究. 下面说说遇到的几个问题. 1.Key不能相同.普通项目 ...