简单的树链剖分+线段树

#include<bits\stdc++.h>
using namespace std;
#define pb push_back
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
const int M=5e5+;
vector<int>g[M];
int tree[M<<],lazy[M<<],top[M],son[M],fa[M],sz[M],dfn[M],to[M],deep[M],cnt,n;
void dfs1(int u,int f){
sz[u]=;
fa[u]=f;
deep[u]=deep[f]+;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(v!=f){
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]])
son[u]=v;
}
}
}
void dfs2(int u,int t){
dfn[u]=++cnt;
to[cnt]=u;
top[u]=t;
if(!son[u])
return ;
dfs2(son[u],t);
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(v!=fa[u]&&v!=son[u])
dfs2(v,v);
}
}
void pushdown(int root){
tree[root<<]=tree[root<<|]=lazy[root]-;
lazy[root<<]=lazy[root<<|]=lazy[root];
lazy[root]=; }
void update(int L,int R,int w,int root,int l,int r){
if(L<=l&&r<=R){
tree[root]=w;
lazy[root]=w+;
return ;
}
if(lazy[root])
pushdown(root);
int midd=(l+r)>>;
if(L<=midd)
update(L,R,w,lson);
if(R>midd)
update(L,R,w,rson);
}
void Update(int u,int v,int w){
while(top[u]!=top[v]){
if(deep[top[u]]<deep[top[v]])
swap(u,v);
update(dfn[top[u]],dfn[u],w,,,n);
u=fa[top[u]];
}
if(deep[u]<deep[v])
swap(u,v);
update(dfn[v],dfn[u],w,,,n);
}
int query(int pos,int root,int l,int r){
if(l==r){
return tree[root];
}
if(lazy[root])
pushdown(root);
int midd=(l+r)>>;
if(pos<=midd)
return query(pos,lson);
if(pos>midd)
return query(pos,rson);
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].pb(v);
g[v].pb(u);
}
dfs1(,);
dfs2(,);
int m;
scanf("%d",&m);
while(m--){
int op,u;
scanf("%d%d",&op,&u);
if(op==){
update(dfn[u],dfn[u]+sz[u]-,,,,n);
}
else if(op==){
Update(,u,);
}
else{
printf("%d\n",query(dfn[u],,,n));
}
}
return ;
}

Codeforces Round #200 (Div. 1)D. Water Tree的更多相关文章

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

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

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

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

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

  5. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  6. Codeforces Round #200 (Div. 1 + Div. 2)

    A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...

  7. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  8. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  9. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

随机推荐

  1. 【每日Scrum】第七天冲刺

    一.计划会议内容 界面ui制作,主界面进度 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图  

  2. SQL server 查询常用语句 2019.3.20

    SQL查询语句 select ...列名 from 表名 投影查询 select sno num,2019-sage as birthday // 给列起别名 from student: 在每个学生姓 ...

  3. 寒假day10

    今天开始写论文,同时爬取并清洗了毕设的人才动态相关部分数据

  4. css伪元素::before与::after使用基础示例

    1.指定文本前后添加内容 <div class="box">test</div> .box::before{ content: 'before'; marg ...

  5. python中selenium自动化常用关键字

    一:定位八种方法 例如: 二:常见的webdriver方法 1.浏览器相关:(打开浏览器先导入webdriver模块) (1)set_window_size(480,800)调整浏览器宽高大小 (2) ...

  6. 题解 P3117 【[USACO15JAN]牛的矩形Cow Rectangles】

    暴力什么的就算了,贪心他不香吗 这题其实如果分开想,就三种情况需要讨论:(由于不会发图,只能手打) 1) 5 . . . . . 4 . . . . . 3 . . . H . 2 . . G . . ...

  7. RegressionTree(回归树)

    1.概述 回归树就是用树模型做回归问题,每一片叶子都输出一个预测值.预测值一般是该片叶子所含训练集元素输出的均值, 即 

  8. python_pycharm控制台输出带颜色

    一.书写格式: 设置颜色开始:\033[显示方式;前景色;背景色m] 结束:\033[0m 二.颜色参数: 前景色 背景色 颜色 ----------------------------------- ...

  9. Python的IDE之Pycharm的使用

    Python的IDE之Pycharm的使用 一.为什么用IDE(Python集成开发环境-Pycharm) 到现在为止,我们也是写过代码的人啦,但你有没有发现,每次写代码要新建文件.写完保存时还要选择 ...

  10. springboot访问请求404问题

    新手在刚接触springboot的时候,可能会出现访问请求404的情况,代码没问题,但就是404. 疑问:在十分确定代码没问题的时候,可以看下自己的包是不是出问题了? 原因:SpringBoot 注解 ...