343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构
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.
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.
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.
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
0
0
0
1
0
1
0
1
题意:
给你一棵树,n个点n-1条边,m个询问
(1)“1 v",表示将以点v为根节点的子树全部赋值为1,
(2)"2 v",表示将点v以及点v的所有祖先节点全部赋值为0,
(3)"3 v",表示查询点v的值。
题解:
我们dfs出dfs序列,再用线段树/树状数组修改一段序列就好了(只有01)
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxn = 5e5+, Pow = << ;
int N, Q, ind[maxn], r[maxn], cur = , a, b;
int emp[*Pow+] , fil[*Pow+];
vector<int> adj[maxn];
void dfs(int v, int par){
ind[v] = cur++;
for(int i = ; i<adj[v].size(); i++) if(adj[v][i]!=par) dfs(adj[v][i], v);
r[v] = cur-;
}
void upd(int l, int r, int t){
l+=Pow; r+=Pow;
while(l<=r){
fil[l] = fil[r] = t;
l = (l+)/;
r = (r-)/;
}
}
int get(int x){
int res = ;
for(int i = Pow+x; i>; i/=) res = max(res, fil[i]);
return res;
}
void add(int x, int t){
for(int i = Pow+x-; i>; i/=) emp[i] = t;
}
int rmq(int p, int l, int r, int a, int b){
if(l>b||r<a) return ;
if(l>=a&&r<=b) return emp[p];
return max(rmq(*p, l, (l+r)/, a, b), rmq(*p+, (l+r)/+, r, a, b));
}
int main(){
ios::sync_with_stdio(false); cin.tie(false); cout.tie(false);
cin >> N;
for(int i = ; i<N-; i++){
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(, -);
cin >> Q;
for(int i = ; i<=Q; i++){
cin >> a >> b;
b--;
if(a==) upd(ind[b], r[b], i);
else if(a==) add(ind[b], i);
else cout << (get(ind[b])>rmq(, , Pow, ind[b], r[b])) << '\n';
}
}
343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构的更多相关文章
- 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/ ...
- 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(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...
- 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的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
随机推荐
- 2014年湖北省TI杯大学生电子设计竞赛论文格式
2014年湖北省TI杯大学生电子设计竞赛 B题:金属物体探測定位器(本科) 2014年8月15日 文件夹 1 系统方案 1.1 XXX的论证与选择........................... ...
- cvReadTrainData
cvReadTrainData的源代码在opencv的cvboost.cpp文件之中,详细内容例如以下所看到的: CV_BOOST_IMPL void cvReadTrainData( const c ...
- 项目部署在windows下的tomcat里
打包放在webapps 目录下,web的改成ROOT ok!!!
- 一个操作oracle的c#类 含分页
有别于以前的一个OracleHelper,这个版各有所长,MARK下. using System; using System.Data; using System.Data.OracleClient; ...
- 如何用php实现qq登陆网站
PHP网站入QQ互联,使用QQ号码登录网站. 平台接口系列文章 PHP网站入QQ互联,使用QQ号码登录网站 PHP网站接入人人网,授权登陆 php facebook api网站接入facebook 1 ...
- oracle故障处理之删除大表空间hang住
背景 数据库分区表数据越来越大,需要对过期话的数据进行迁移,以及大的分区表需要进行数据的清理和删除,达到释放磁盘空间的目的. 问题说明 环境:linux 6.X 数据库:oracle 11.2.0.4 ...
- ios 中生成随机数
ios 有如下三种随机数方法: 1. srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; 2. s ...
- 安卓input框获取焦点时,底部按钮会顶上去的解决方法
var h = document.body.scrollHeight;window.onresize = function(){ if (document.body.scrollHeight < ...
- 使用最新vue_cli+webpack搭建的模版
使用最新vue_cli+webpack搭建的模版,包含了常用的插件,router和axiox与测试插件.项目的结构如下: 框架下载地址:https://share.weiyun.com/5Cl7EbU
- 新物理AI将可能成为量子计算革命的关键
新物理AI将可能成为量子计算革命的关键 据外媒报道,量子计算无疑是现在最令人兴奋的技术之一,但它的量子物理基础却让它成为了一个令人讨厌的概念理解甚至很难再展开其他事情.然而,最近物理学研究的一项突破可 ...