Codeforces 343D Water Tree
题意简述
维护一棵树,支持以下操作:
- 0 v:将以v为跟的子树赋值为1
- 1 v:将v到根节点的路径赋值为0
- 2 v:询问v的值
题解思路
树剖+珂朵莉树
代码
#include <set>
#include <cstdio>
#define IT std::set<Node>::iterator
const int N=500005;
int n,q,u,v,opt,x,cnt;
int h[N],to[N<<1],nxt[N<<1];
int fa[N],sz[N],hvs[N],id[N],top[N];
struct Node {
int l,r; bool v;
Node(const int& L,const int& R=-1,const bool& V=0):l(L),r(R),v(V) {}
bool operator <(const Node& x) const { return l<x.l; }
};
inline void add_edge(const int& u,const int& v) {
to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt;
}
void dfs1(const int& u) {
sz[u]=1;
for (register int i=h[u];i;i=nxt[i])
if (to[i]^fa[u]) {
fa[to[i]]=u;
dfs1(to[i]);
sz[u]+=sz[to[i]];
if (sz[to[i]]>sz[hvs[u]]) hvs[u]=to[i];
}
}
void dfs2(const int& u) {
id[u]=++cnt;
if (hvs[u]) { top[hvs[u]]=top[u]; dfs2(hvs[u]); }
for (register int i=h[u];i;i=nxt[i])
if (to[i]!=hvs[u]&&to[i]!=fa[u]) {
top[to[i]]=to[i]; dfs2(to[i]);
}
}
std::set<Node> s;
inline IT split(const int& pos) {
IT it=s.lower_bound(Node(pos,0,0));
if (it!=s.end()&&it->l==pos) return it;
--it;
const int L=it->l,R=it->r; const bool V=it->v;
s.erase(it);
s.insert(Node(L,pos-1,V)); return s.insert(Node(pos,R,V)).first;
}
inline void assign(const int& l,const int& r,const bool& va) {
IT itr=split(r+1),itl=split(l); s.erase(itl,itr); s.insert(Node(l,r,va));
}
inline void modify(int u) {
for (;top[u]!=1;u=fa[top[u]]) assign(id[top[u]],id[u],0); assign(1,id[u],0);
}
inline int query(int x) { IT it=split(x); return it->v; }
int main() {
scanf("%d",&n); s.insert(Node(1,n,0));
for (register int i=1;i<n;++i)
scanf("%d%d",&u,&v),add_edge(u,v),add_edge(v,u);
cnt=0; fa[1]=top[1]=1; dfs1(1); dfs2(1);
scanf("%d",&q);
for (register int i=1;i<=q;++i) {
scanf("%d%d",&opt,&x);
if (opt==1) assign(id[x],id[x]+sz[x]-1,1);
else if (opt==2) modify(x);
else printf("%d\n",query(id[x]));
}
}
Codeforces 343D Water Tree的更多相关文章
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- Codeforces 343D Water Tree & 树链剖分教程
原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题 ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- 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 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 ...
随机推荐
- C语言学习书籍推荐《C和指针 Pointers On C》下载
<C和指针 POINTERS ON C>提供与C语言编程相关的全面资源和深入讨论.本书通过对指针的基础知识和高 级特性的探讨,帮助程序员把指针的强大功能融入到自己的程序中去. 全书共18 ...
- Kafka FAQ
报错如下: Unable to read additional data from client sessionid 0x15d2c867a770006 使用的kafka自带的zookeeper,测试 ...
- Windows Presentation Foundation (WPF) 项目中不支持xxx的解决
一般Windows Presentation Foundation (WPF) 项目中不支持xxx都是由于没引用相应的程序集导致,比如Windows Presentation Foundation ( ...
- easypermissions的基本使用
转载请注明出处 http://blog.csdn.net/pyfysf/article/details/78204395 Android 6.0(API 级别 23)开始,在应用运行时向其授予权限,而 ...
- 常用socket函数详解
常用socket函数详解 关于socket函数,每个的意义和基本功能都知道,但每次使用都会去百度,参数到底是什么,返回值代表什么意义,就是说用的少,也记得不够精确.每次都查半天,经常烦恼于此.索性都弄 ...
- MySQL不停地自动重启怎么办
近期,测试环境出现了一次MySQL数据库不断自动重启的问题,导致的原因是强行kill -9 杀掉数据库进程导致,报错信息如下: --24T01::.769512Z [Note] Executing ' ...
- springboot的邮件服务
作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权归作者所有,转载请注明出处 springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEA ...
- Python基础总结之第九天开始【python之OS模块对目录的操作、以及操作文件】(新手可相互督促)
年薪20万的梦想... python对文件.目录能做什么?或者说我们需要python替我们做什么?最经常的操作就是对文件的:打开.关闭.读取.写入.修改.保存等等对目录的操作,无非就是 ...
- 线上调试bug
在以往的工作中,线上一有bug,就需要把文件弄到本地来改,但经常会碰见本地环境又和线上不一样,导致调试困难,闭着眼睛改好之后传到线上去看对不对,不对的话又要改,循环往复,要多麻烦就有多麻烦啊. 今天给 ...
- 设置Myeclipse的jvm内存参数
Myeclipse经常会遇到内存溢出和Gc开销过大的情况,这时候就需要修改Myeclipse的Jvm内存参数 修改如下:(使用Extjs做公司大项目时候,不要让项目Builders的Javascrip ...