题意简述

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

  • 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的更多相关文章

  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. windows美化工具7+ Taskbar Tweaker

    今天分享一个windows美化工具 7+ Taskbar Tweaker 调整工具专为 Windows 任务栏工作者量身定制,支持 Windows 7 以及更高版本的(非服务器版)微软操作系统平台. ...

  2. Windows下必备的开发神器之Cmder使用说明

    诚言,对于开发码字者,Mac和Linux果断要比Windows更贴心;但只要折腾下,Windows下也是有不少利器的.之前就有在Windows下效率必备软件一文中对此做了下记载:其虽没oh-my-zs ...

  3. NioEventLoop启动流程源码解析

    NioEventLoop的启动时机是在服务端的NioServerSocketChannel中的ServerSocketChannel初始化完成,且注册在NioEventLoop后执行的, 下一步就是去 ...

  4. 【动态规划法(DP)】-C++

    360百科定义: 动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法.20世纪50年代初美国数学家R.E.Bell ...

  5. ubuntu18.04安装nvidia驱动总结经验

    本人电脑是 DELL Inspiron 3670, 系统装的是ubuntu18.04, 显卡使用的是GeForce GTX 1050 Ti, 在安装nividia显卡的时候花费两天时间,感受颇深,顾总 ...

  6. 个人永久性免费-Excel催化剂插件功能修复与更新汇总篇之九

    第11波-快速批量插入图片并保护纵横比不变 原文链接:https://www.jianshu.com/p/9a3d9aa7ba7e 修复了插入图片有纵向的图片时,插入后还是显示横向的情况. 第83波- ...

  7. 阿里百川HotFix2.0热修复初体验

    博客原地址:http://blog.csdn.net/allan_bst/article/details/72904721 一.什么是热修复 热修复说白了就是"打补丁",比如你们公 ...

  8. element 上传组件 el-upload 的经验总结

    前言 最近在做后台管理项目,采用的 vue-element-admin ,上传图片是一个很常用的功能,也遇到了很多问题,刚好趁此机会做一些总结. 初步总结下会提到的问题,目录如下: el-upload ...

  9. 2019前端面试系列——Vue面试题

    Vue 双向绑定原理        mvvm 双向绑定,采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty()来劫持各个属性的 setter.getter,在数 ...

  10. ajax性能优化

    ajax性能优化 例: 模块: A B C D 开销: 50%  3% 25%   22% 如果我们优化B就如同那些那些只执行一次的代码,性能·提高不到哪里去:反之,我们去优化A,比如去优化它的循环, ...