Codeforces Round #200 (Div. 1)D. Water Tree dfs序
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:
- 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 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.
Bi,Ci,即此题的初始分值、每分钟减少的分值、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序的更多相关文章
- 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 ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- 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 ...
- Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)
https://codeforces.com/contest/1062/problem/E 题意 给一颗树n,然后q个询问,询问编号l~r的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
随机推荐
- mac tree命令
mac下默认是没有 tree命令的,不过我们可以使用find命令模拟出tree命令的效果,如显示当前目录的 tree 的命令: $ find . -print | sed -e 's;[^/]*/;| ...
- p2p穿透技术
ios 怎么和wifi外设摄像头实时传输视频 ios 控制wifi摄像头外设的拍照.录像.删除照片等等都可以通过tcp/ip 发送定义好的json指令实现. 但是不知道怎么和wifi外设摄像头实时传输 ...
- Jquery动画第一部分
效果图: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.asp ...
- TRSWCM学习问题总结
1,置标属性"id"是用来制定调用那个栏目的数据(全字配备,可以文字匹配好奇怪,好不专业.所以建议创建栏目或者站点的时候,将唯一标识设置成英文,这样才符合程序比对习惯) 2,需要添 ...
- javascript中this的使用
终于知道某些大神在写js插件的时候为什么第一句都是"var that=this",来看看下面的这个例子,大家都会懂啦: <script type="text/jav ...
- windows官方多语言方案
编写 Win32 多语言用户界面应用程序 Windows 2000 针对全球市场制定了新的增强支持标准,提供了许多国际化功能,例如完全支持 Unicode.预设支持数百种语言以及用于从右向左语言的镜像 ...
- C# 判断两张图片是否一致的快速方法
#region 判断图片是否一致 /// <summary> /// 判断图片是否一致 /// </summary> /// <param name="img& ...
- 新功能:Azure Traffic Manager 嵌套配置文件
Jonathan Tuliani Azure 网络 - DNS 和 Traffic Manager 项目经理 我们很高兴地宣布,Azure Traffic Manager 支持 Traffic Ma ...
- 【转】iOS中16进制转10进制
原文网址:http://www.voidcn.com/blog/u012198553/article/p-4976772.html /// 将十六进制的字符串转化为NSData - (NSData ) ...
- C# 等待另外一个窗体关闭,再进行主线程的代码
方法1 用Form类或其子类的showDialog方法. 比如你在form1里有一个按扭,然后你在Form1的点击事件里写上显示form2的代码: Form2 frm=new Form2(); frm ...