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的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
随机推荐
- ASP.NE的缓存技术提高Web站点的性能
一:我们为什么要使用缓存? 先来理解一下asp.net缓存技术的基本原理:把访问频繁的数据以及需要花大量的时间来加载的数据缓存在内存中,那么用户在下次请求同样的数据时,直接将内存中的数据返回给用户,从 ...
- CQOI2011分金币&HAOI2008糖果传递
双倍经验…… 没想到白书上竟然有……我还看过……还忘了…… 抄份题解: A1 + X1 - X2 = G A2 + X2 - X3 = G . . . An + Xn - X1 = G 解得 X1 = ...
- Vim Vundle 插件管理器
/********************************************************************** * Vim Vundle 插件管理器 * 说明: * 话 ...
- JavaEE基本了解
1. 为什么需要JavaEE 我们编写的JSP代码中,由于大量的显示代码和业务逻辑混淆在一起,彼此嵌套,不利于程序的维护和扩展.当业务需求发生变化的时候,对于程序员和美工都是一个很重的负担. 为了程 ...
- 用VisualSVN做项目版本控制
一.SVN服务端 1.VisualSVN Server下载: http://download.csdn.net/detail/jiminull/4448874 或 http://www.visuals ...
- 【转】Linux(Ubuntu)下面SecureCRT 完全破解
仅供测试, 勿用作商业用途.首先要到vandyke网站下载一个securecrt, 需要注册.http://www.vandyke.com/download/securecrt/download.ht ...
- Zabbix探索:资产信息的妙用
前一阵子还在考虑CMDB的问题,因此Zabbix中的Inventory,也就是所谓的资产信息,遭到了我的不少鄙视. 这几天在研究告警通知对应责任人的问题,突然想起Zabbix的资产信息中应该有这么一栏 ...
- [娱乐]GameMaker绘制参数方程的图像
今天,我翻了旧物,硬着头皮看了这源码.突然恍然大悟,这岂不就是当年学的参数方程! 目前,最早开始教授参数方程实在高三时,并作为一门选修课程,简化了求解圆锥曲线方程的难度,在高考中也很容易拿分,考试过后 ...
- 【转】ExcelHelper类,用npoi读取Excel文档
//------------------------------------------------------------------------------------- // All Right ...
- 恒天云技术分享系列3 – KVM性能调优
恒天云技术分享:http://www.hengtianyun.com/download-show-id-11.html KVM是什么 KVM 是 kernel-based Virtual Machin ...