题意简述

维护一棵树,支持以下操作:

  • 0 v:将以v为跟的子树赋值为1
  • 1 v:将v到根节点的路径赋值为0
  • 2 v:询问v的值

题解思路

树剖+珂朵莉树

代码

  1. #include <set>
  2. #include <cstdio>
  3. #define IT std::set<Node>::iterator
  4. const int N=500005;
  5. int n,q,u,v,opt,x,cnt;
  6. int h[N],to[N<<1],nxt[N<<1];
  7. int fa[N],sz[N],hvs[N],id[N],top[N];
  8. struct Node {
  9. int l,r; bool v;
  10. Node(const int& L,const int& R=-1,const bool& V=0):l(L),r(R),v(V) {}
  11. bool operator <(const Node& x) const { return l<x.l; }
  12. };
  13. inline void add_edge(const int& u,const int& v) {
  14. to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt;
  15. }
  16. void dfs1(const int& u) {
  17. sz[u]=1;
  18. for (register int i=h[u];i;i=nxt[i])
  19. if (to[i]^fa[u]) {
  20. fa[to[i]]=u;
  21. dfs1(to[i]);
  22. sz[u]+=sz[to[i]];
  23. if (sz[to[i]]>sz[hvs[u]]) hvs[u]=to[i];
  24. }
  25. }
  26. void dfs2(const int& u) {
  27. id[u]=++cnt;
  28. if (hvs[u]) { top[hvs[u]]=top[u]; dfs2(hvs[u]); }
  29. for (register int i=h[u];i;i=nxt[i])
  30. if (to[i]!=hvs[u]&&to[i]!=fa[u]) {
  31. top[to[i]]=to[i]; dfs2(to[i]);
  32. }
  33. }
  34. std::set<Node> s;
  35. inline IT split(const int& pos) {
  36. IT it=s.lower_bound(Node(pos,0,0));
  37. if (it!=s.end()&&it->l==pos) return it;
  38. --it;
  39. const int L=it->l,R=it->r; const bool V=it->v;
  40. s.erase(it);
  41. s.insert(Node(L,pos-1,V)); return s.insert(Node(pos,R,V)).first;
  42. }
  43. inline void assign(const int& l,const int& r,const bool& va) {
  44. IT itr=split(r+1),itl=split(l); s.erase(itl,itr); s.insert(Node(l,r,va));
  45. }
  46. inline void modify(int u) {
  47. for (;top[u]!=1;u=fa[top[u]]) assign(id[top[u]],id[u],0); assign(1,id[u],0);
  48. }
  49. inline int query(int x) { IT it=split(x); return it->v; }
  50. int main() {
  51. scanf("%d",&n); s.insert(Node(1,n,0));
  52. for (register int i=1;i<n;++i)
  53. scanf("%d%d",&u,&v),add_edge(u,v),add_edge(v,u);
  54. cnt=0; fa[1]=top[1]=1; dfs1(1); dfs2(1);
  55. scanf("%d",&q);
  56. for (register int i=1;i<=q;++i) {
  57. scanf("%d%d",&opt,&x);
  58. if (opt==1) assign(id[x],id[x]+sz[x]-1,1);
  59. else if (opt==2) modify(x);
  60. else printf("%d\n",query(id[x]));
  61. }
  62. }

Codeforces 343D Water Tree的更多相关文章

  1. 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 ...

  2. CodeForces 343D water tree(树链剖分)

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  3. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  4. Codeforces 343D Water Tree & 树链剖分教程

    原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题 ...

  5. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  6. 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 ...

  7. 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/ ...

  8. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  9. 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 ...

随机推荐

  1. C语言学习书籍推荐《C和指针 Pointers On C》下载

    <C和指针 POINTERS ON C>提供与C语言编程相关的全面资源和深入讨论.本书通过对指针的基础知识和高 级特性的探讨,帮助程序员把指针的强大功能融入到自己的程序中去.  全书共18 ...

  2. Kafka FAQ

    报错如下: Unable to read additional data from client sessionid 0x15d2c867a770006 使用的kafka自带的zookeeper,测试 ...

  3. Windows Presentation Foundation (WPF) 项目中不支持xxx的解决

    一般Windows Presentation Foundation (WPF) 项目中不支持xxx都是由于没引用相应的程序集导致,比如Windows Presentation Foundation ( ...

  4. easypermissions的基本使用

    转载请注明出处 http://blog.csdn.net/pyfysf/article/details/78204395 Android 6.0(API 级别 23)开始,在应用运行时向其授予权限,而 ...

  5. 常用socket函数详解

    常用socket函数详解 关于socket函数,每个的意义和基本功能都知道,但每次使用都会去百度,参数到底是什么,返回值代表什么意义,就是说用的少,也记得不够精确.每次都查半天,经常烦恼于此.索性都弄 ...

  6. MySQL不停地自动重启怎么办

    近期,测试环境出现了一次MySQL数据库不断自动重启的问题,导致的原因是强行kill -9 杀掉数据库进程导致,报错信息如下: --24T01::.769512Z [Note] Executing ' ...

  7. springboot的邮件服务

    作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权归作者所有,转载请注明出处 springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEA ...

  8. Python基础总结之第九天开始【python之OS模块对目录的操作、以及操作文件】(新手可相互督促)

    年薪20万的梦想...         python对文件.目录能做什么?或者说我们需要python替我们做什么?最经常的操作就是对文件的:打开.关闭.读取.写入.修改.保存等等对目录的操作,无非就是 ...

  9. 线上调试bug

    在以往的工作中,线上一有bug,就需要把文件弄到本地来改,但经常会碰见本地环境又和线上不一样,导致调试困难,闭着眼睛改好之后传到线上去看对不对,不对的话又要改,循环往复,要多麻烦就有多麻烦啊. 今天给 ...

  10. 设置Myeclipse的jvm内存参数

    Myeclipse经常会遇到内存溢出和Gc开销过大的情况,这时候就需要修改Myeclipse的Jvm内存参数 修改如下:(使用Extjs做公司大项目时候,不要让项目Builders的Javascrip ...