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的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
随机推荐
- Android开发之View动画效果插补器Interpolator
插补器Interpolator 官网描述:An interpolator defines the rate of change of an animation. This allows the bas ...
- Java设计模式之工厂模式(简单工厂模式+工厂方法模式)
摘自http://blog.csdn.net/jason0539/article/details/23020989 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是 ...
- poj3308Paratroopers(dinic)
http://poj.org/problem?id=3308 给两个定义 最小割:对于图中的两个点(一般为源点和汇点)来说,如果把图中的一些边去掉,如果它们之间无法连通的话,则这些边组成的集合就叫为割 ...
- WordPress 3.8.1 /xmlrpc.php拒绝服务漏洞
漏洞版本: WordPress 3.8.1 漏洞描述: WordPress是一款内容管理系统. WordPress 3.8.1 /xmlrpc.php 文件有ping其他主机的功能,通过这个功能可以请 ...
- unix network programming(3rd)Vol.1 [第13~15章]《读书笔记系列》
第13章 守护进程和inetd 超级服务器 syslog() daemon_init() setuid() setgid() 第14章 高级IO 标准I/O函数库,支持3种缓冲 缓冲(读写存储设备(硬 ...
- 17、手势(Gesture)
课程目标: 学习Android必不可少的手势的功能 了解手势识别原理 , 掌握制作,加载以及识别手势 写出自己的手势Demo 重点难点:手势机制的了解 手势库的制作 考核目标:请说一下手势库的 ...
- 求一字符串最长不重复字符子串的长度【Java 版】
一. 前言 最近学习有点断断续续,整理的一些知识点要么不完整,要么完全没搞懂,不好拿上台面,还是先在草稿箱躺着吧.偶尔在浏览大牛博客http://coolshell.cn的时候,发现大牛业余时间也在做 ...
- WebService的发布及客户端的调用
一.目录 1.JAX-WS发布WebService 1.1 创建一个简单的WS 1.2 打包部署和发布 2.CXF+Spring发布WebService 3.客户端的调用方式 二.正文 1. JAX- ...
- Ubuntu 14.04 设置静态IP
使用Network Manager UI界面中指定 手动时,无法保存. 通过修改配置文件解决来此问题.记录以下. 如果输入过密码后,就会出现在这个目录下面, 以如下chinaNet为例 gaojing ...
- POJ 3321 Apple Tree(dfs序树状数组)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...