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的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...
随机推荐
- Android 零基础学习之路
第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正則表達式. 3.面向对象的抽象.封装,继承,多态.类与对象.对象初始化 ...
- UESTC--1263--The Desire of Asuna(贪心)
The Desire of Asuna Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Su ...
- Oracle 优化和性能调整
分析评价Oracle数据库性能主要有数据库吞吐量.数据库用户响应时间两项指标.数据库用户响应时间又可以分为系统服务时间和用户等待时间两项,即: 数据库用户响应时间=系统服务时间+用户等待时间 因此 ...
- weblogic管理脚本
start.sh Java代码 #!/usr/bin/bash # # start.sh # @auth: zhoulin@lianchuang.com # SERVER_STATUS () { s ...
- IPython Autoreload
在PyCharm中进行代码调试的时候, 设置修改的模块自动重新载入是非常方便的 In [1]: %load_ext autoreload In [2]: %autoreload 2
- SVN——Jenkins自动发布
最近公司项目处于开发阶段,很多功能开发完后就需要发布到测试环境等待测试去验收,这个时候如果手动更新网站的话,是很费时费力的. 于是乎,我们做成了自动发布,这样我们只管提交代码到SVN就行了,发布由软件 ...
- CentOS 5/6 下添加epel源
如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 Fedora Project 推出的 EPEL(Extra Packages for Enter ...
- GRpc-Proto3语法
syntax = "proto3"; 文件的第一行指定了你使用的是proto3的语法:如果你不指定,protocol buffer 编译器就会认为你使用的是proto2的语 ...
- windows下git server搭建
使用gitblit1.8搭建 首先要安装JDK 然后下载gitblit,这里给一个CSDN下载 https://download.csdn.net/download/nietzsche0/104826 ...
- mongodb配置文件详解
logpath=/app/mongo/mongolog/mongo.log dbpath=/app/mongo/mongodata verbose = true #vvvv = true #此项会产生 ...