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的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
随机推荐
- linux下tar压缩/解压的使用(tar) 压缩/解压
压缩: tar -zcvf 压缩后文件名.tar.gz 被压缩文件 解压: tar -zxvf 被解压文件 具体的可以在linux环境下 用 tar --help 查看详细说明格式:ta ...
- Java面试题-多线程
1. java中有几种方法可以实现一个线程? 多线程有两种实现方法,分别是继承Thread类与实现Runnable接口. 这两种方法的区别是,如果你的类已经继承了其它的类,那么你只能选择实现Runna ...
- 函数buf_LRU_free_from_common_LRU_list
/******************************************************************//** Try to free a clean page fro ...
- bzoj1293: [SCOI2009]生日礼物
单调队列 用一个堆维护目前每个颜色在里面的点,每回取出队首点,并更新答案.一旦哪个颜色的点都被用完,跳出循环. #include<cstdio> #include<algorithm ...
- 使用LINQ 對List分頁/區
listview之類的服務器控件分頁自不用多說,拖拖控件改改屬性分分鐘的事.就不浪費大家時間了. 這裏只寫大概思路及關鍵代碼了. LINQ裏有一個對集合進行分區的操作可用於分頁. page ...
- Highcharts20151130
$(function () { $('#container').highcharts({ chart: { type: 'spline' // 图的类型 }, title: { text: null ...
- apache开源项目--Shiro
安全是企业应用中不可缺少的功能,在众多权限框架中,Shiro(其前身是JSecurity)因其简单而又不失强大的特点引起了不少开发者的注 意.随着Grails的关注度越来越高,在Grails社区也出现 ...
- jquery M97-datepicker日历控件
My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 2.在页面中引入该组件js文件: <script type=&quo ...
- erlang学习笔记之基础语法
字符串是双引号,单引号的是atom元组: 下标从1开始 X = {'test1',2,3,4}. element(1,X). 配合模式匹配,可以给元素项命名,直接不用下标标记元素项 列表增删改查 增加 ...
- 我的WCF之旅(3):在WCF中实现双工通信
双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...