CoderForces343D: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.
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.
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.
Example
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
题意:给定一棵树,没棵树上有一个水池,现在有如下操作,
1,给V和其子树装水。
2,对V和其祖先排水。
3,问V是否有水。
思路1:看到操作1,操作子树,很自然地想到dfs序;然后但是2操作,V及其祖先在线段树上的位置是离散的,所以需要暴力。
转化一下思路:本来是多点更改2,单点查询3; 改为:单点更新2,区间查询3。
即:3操作改为,查询V及其子树是否都有水。
注意:对1操作,假设V的子树有空(存在无水的点)的,则需要把V的父亲改为空。 因为V的子树有空,则V的父亲为空,而且给V和子树加水后V父亲任然为空。但是由于没有单点更改V的父亲,而V的父亲和其子树都满水,则导致误判以为V的父亲有水。 而V的父亲最具有代表性,只改V的父亲节点即可。
思路2:2操作用树链剖分,避免了暴力修改,效率还过得去。
(下面的思路1的代码)
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cnt;
int fa[maxn],in[maxn],out[maxn],times;
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int pre)
{
fa[u]=pre; in[u]=++times;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre) dfs(v,u);
} out[u]=times;
}
int Lazy[maxn<<],all[maxn<<];
struct DFSTree{
void pushdown(int Now)
{
if(Lazy[Now]){
Lazy[Now]=;
all[Now<<]=; all[Now<<|]=;
Lazy[Now<<]=; Lazy[Now<<|]=;
}
}
void pushup(int Now)
{
all[Now]=all[Now<<]&all[Now<<|];
}
void add(int Now,int L,int R,int l,int r)
{ if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) add(Now<<,L,Mid,l,r);
if(r>Mid) add(Now<<|,Mid+,R,l,r);
pushup(Now);
}
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return all[Now];
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid){ if(!query(Now<<,L,Mid,l,r)) return ;}
if(r>Mid) { if(!query(Now<<|,Mid+,R,l,r)) return ;}
pushup(Now);
return ;
}
void del(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) del(Now<<,L,Mid,l,r);
if(r>Mid) del(Now<<|,Mid+,R,l,r);
/*else{
del(Now<<1,L,Mid,l,r);
del(Now<<1|1,Mid+1,R,l,r);
} */
pushup(Now);
}
}Tree;
int main()
{
int N,Q,u,v,i,j,opt;
scanf("%d",&N);
for(i=;i<N;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(,);
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&opt,&u);
if(opt==){
if(!Tree.query(,,N,in[u],out[u]))
Tree.del(,,N,in[fa[u]],in[fa[u]]);
Tree.add(,,N,in[u],out[u]);
}
else if(opt==) Tree.del(,,N,in[u],in[u]);
else printf("%d\n",Tree.query(,,N,in[u],out[u]));
}
return ;
}
加了输入优化后排第二了。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cnt;
int fa[maxn],in[maxn],out[maxn],times;
void read(int &res){
char c=getchar();
for(;c>''||c<'';c=getchar());
for(res=;c>=''&&c<='';c=getchar()) res=(res<<)+(res<<)+c-'';
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int pre)
{
fa[u]=pre; in[u]=++times;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre) dfs(v,u);
} out[u]=times;
}
int Lazy[maxn<<],all[maxn<<];
struct DFSTree{
void pushdown(int Now)
{
if(Lazy[Now]){
Lazy[Now]=;
all[Now<<]=; all[Now<<|]=;
Lazy[Now<<]=; Lazy[Now<<|]=;
}
}
void pushup(int Now)
{
all[Now]=all[Now<<]&all[Now<<|];
}
void add(int Now,int L,int R,int l,int r)
{ if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) add(Now<<,L,Mid,l,r);
if(r>Mid) add(Now<<|,Mid+,R,l,r);
pushup(Now);
}
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return all[Now];
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) if(!query(Now<<,L,Mid,l,r)) return ;
if(r>Mid) if(!query(Now<<|,Mid+,R,l,r)) return ;
pushup(Now);
return ;
}
void del(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) del(Now<<,L,Mid,l,r);
if(r>Mid) del(Now<<|,Mid+,R,l,r);
pushup(Now);
}
}Tree;
int main()
{
int N,Q,u,v,i,j,opt;
read(N);
for(i=;i<N;i++){
read(u); read(v);
add(u,v); add(v,u);
}
dfs(,);
read(Q);
while(Q--){
read(opt); read(u);
if(opt==){
if(!Tree.query(,,N,in[u],out[u]))
Tree.del(,,N,in[fa[u]],in[fa[u]]);
Tree.add(,,N,in[u],out[u]);
}
else if(opt==) Tree.del(,,N,in[u],in[u]);
else printf("%d\n",Tree.query(,,N,in[u],out[u]));
}
return ;
}
CoderForces343D:Water Tree(dfs序+线段树&&特殊处理)的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ3321 - Apple Tree DFS序 + 线段树或树状数组
Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...
- 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 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- codechef T6 Pishty and tree dfs序+线段树
PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...
- codeforces 620E. New Year Tree dfs序+线段树+bitset
题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...
- CodeForces 620E:New Year Tree(dfs序+线段树)
E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- 【cf343】D. Water Tree(dfs序+线段树)
传送门 题意: 给出一个以\(1\)为根的有根树,起始每个结点都为\(0\),现在有三种操作: 1.将\(v\)及\(v\)的子树都置为\(1\): 2.将\(v\)及其所有的祖先都置为\(0\): ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
随机推荐
- Linux主机被SSH精神病(Psychos)暴力攻破后成为肉鸡的攻防过程
近日公司局域网突然变得非常慢,上网受到很大影响,不仅仅是访问互联网慢,就连访问公司内部服务器都感到异常缓慢.于是对本局域网网关进行测试: $ ping 10.10.26.254 发现延时很大, ...
- idea tomcat 配置
昨天我们讲了如何新建多模块项目:idea创建maven多模块项目 本节课,我们讲如何配置tomcat,使昨天配置的web项目,JRapid.Admin可以运行起来.具体步骤如下 第一步 第二步 第三步 ...
- discuz搬家
1.需要重命名forumdata下面的cache和templates文件夹 2.如果数据库账户名和密码有改动过,需要修改config.inc.php 3.保证uc_server的链接正确!
- Python机器学习--回归
线性回归 # -*- coding: utf-8 -*- """ Created on Wed Aug 30 19:55:37 2017 @author: Adminis ...
- 在canvas上面拖拽对象。
原文:https://html5.litten.com/how-to-drag-and-drop-on-an-html5-canvas/ 下面作者的原始的版本会抖动一下(鼠标刚点下去的时候,位置会发生 ...
- javaweb 中的过滤器 包装器
过滤器要做的事情: 请求过滤器:完毕安全检查,又一次格式化请求首部或体.建立请求审计或日志 响应过滤器: 压缩响应流,追加或改动响应流创建一个全然不同的响应. 过滤器和servlet三个相似地 ...
- ormlite
id 主键 默认为false generatedId 自增长的主键 默认值是false generatedIdSequence 字符串名称的序列号 类同generatedId,但您可以指定序列的名称使 ...
- UFLDL教程笔记及练习答案三(Softmax回归与自我学习***)
:softmax回归 当p(y|x,theta)满足多项式分布,通过GLM对其进行建模就能得到htheta(x)关于theta的函数,将其称为softmax回归. 教程中已经给了cost及gradie ...
- 使用 Docker 在 Linux 上托管 ASP.NET Core 应用程序
说在前面 在阅读本文之前,您必须对 Docker 的中涉及的基本概念以及常见命令有一定了解,本文侧重实战,不会对相关概念详述. 同时请确保您本地开发机器已完成如下安装: Docker 18.06 或更 ...
- ios开发之自己定义默认生成的导航栏 标题 颜色 返回button
一 改动导航栏颜色 导航栏在哪个页面代码放在那里面 self.navigationController.navigationBar.tintColor = [UIColor colorWithR ...