简单的树链剖分+线段树

#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. 2.Git基本配置

    用户名和邮箱地址是本地git客户端的一个变量 . 用户每次提交代码都会记录用户名和邮箱 . 设置git的用户和邮箱git config [--local | --global | --system] ...

  2. 监听home键的广播

    public class HomeKeyReceiver extends BroadcastReceiver implements SanbotConstants{ private HomeKeyLi ...

  3. sublime中设置scala编译运行

    Attention: 前提.前提.前提:电脑上安装scala和jdk,可以在cmd中运行scala命令: okay,next: 1.配置内容 在Tools->Build Systems-> ...

  4. h5-localStorage储存的使用

    <!-- localStorage的使用: 1.存储的内容大概20mb 2.不同浏览器不能共享数据,但是在同意浏览器的不同窗口中可以共享数据 3.永久生效,他的数据是储存在硬盘上,并不会随着页面 ...

  5. memset的常见用法

    头文件 <cstring> 描述 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组 ------------------------------------ ...

  6. 半监督的GAN算法

    ImprovedGAN $ Loss = Loss_{supervised}   + \lambda * Loss_{unsupervised} $ 第二项形式与原始的GAN模型类似. 参考: Imp ...

  7. java生成6位数所有组合

    for(int i=0;i<=9;i++){ String str=""; str=str+i; String strj=""; for(int j=0; ...

  8. PAT甲级——1073 Scientific Notation (20分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  9. keras猫狗大战

    先划分数据集程序训练集中猫狗各12500张现在提取1000张做为训练集,500张作为测试集,500张作为验证集: # -*- coding: utf-8 -*-import os, shutil or ...

  10. ubuntu编译caffe遇到的问题及解决方案

    问题1 /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or ...