简单的树链剖分+线段树

#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. python + selenium +win32gui + winspy 实现图片上传

    过程:模拟点击上传按钮,打开Windows对话框,编辑栏输入文件路径(或网址)点击确定.网上随便找了一个进行测试. 点击后出现Windows上传对话框 用 winspy 来检测窗口的句柄 python ...

  2. UVALive 3704 细胞自动机 矩阵快速幂

    是时候要做做数学类的题目了 这属于比较简单的矩阵快速幂了,因为有个已知的矩阵循环的结论,所以为了节约时空,只需要保留一行即可,这个稍微有点难写,也不是难写,主要是注意细节.其他的矩阵快速幂一下即可 # ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单验证

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. libcurl在windows下的使用

    curl在linux下很好用,但到了windows下写程序却没办法使用了,这时候可以使用libcurl库 libcurl库的编译网上很多,我就不一一赘述了,curl的官方网站:https://curl ...

  5. ubuntu 插网线无法上网解决方案

    前言 不知道最近是什么情况,ubuntu链接网线总是上不去网,但是wifi还能用,一直也就没有捣鼓,不过今天连wifi都不能用了,只能开始修理了. 修复方案 使用ifconfig命令查看以太网的名称 ...

  6. leetcode 690.员工的重要性

    题目: 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结 ...

  7. CSS中的 position与Grid Layout

    [1]CSS之Position(定位布局): 现css常用的属性有5种: 1.static 2.absolute 3.fixed 4.relative 5.sticky. 1.static 表示元素的 ...

  8. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

  9. 201412-1 门禁系统 Java

    类似于201312-1 出现次数最多的数,了解一下Map的containsKey方法和containsValue方法 **containsKey** boolean containsKey(Objec ...

  10. ruoyi LogUtils

    package com.ruoyi.framework.util; import java.io.PrintWriter; import java.io.StringWriter; import ja ...