Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树
#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的更多相关文章
- 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/ ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- 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 ...
- 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 ...
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- Codeforces Round #200 (Div. 1 + Div. 2)
A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- HDU_2256 矩阵快速幂 需推算
最近开始由线段树转移新的内容,线段树学到扫描线这里有点迷迷糊糊的,有时候放一放可能会好一些. 最近突然对各种数学问题很感兴趣.好好钻研了一下矩阵快速幂.发现矩阵真是个计算神器,累乘类的运算原本要O(N ...
- 干货 | 京东云托管Kubernetes集成镜像仓库并部署原生DashBoard
在上一篇"Cloud Native 实操系列"文章中,我们为大家介绍了如何通过京东云原生容器实现Eureka的部署(
- C++queue的使用
C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...
- UIWindow statusBar消失
1.新建UIWindow 程序崩溃 报无根控制器错误 Xcode7环境下,新建UIWindow需添加rootViewController 2.新建UIWindow后 statusBar消失 Info. ...
- TextView和Button的学习
常用属性,界面跳转,按钮学习,按压颜色的变换,图片的插入学习等 工程目录: MainActivity.java: package com.example.revrse; import androidx ...
- 学生信息管理系统java测试报告
package studentinformation; /**姓名 胡海靖 * 学号 20183609 * 班级 信1805-2 */ class ScoreInformation { private ...
- php 随机useragent
<?php /** * 获取随机useragent */ private function get_rand_useragent($param) { $arr = array( 'Mozilla ...
- 查看jks文件中的签名
1. 打开CMD命令行进入本机安装的jdk或jre下的bin目录. 2. 下来看图 keytool -list -v -keystore C:\Users\Administrator\Desktop\ ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- Ubuntu python多个版本管理
1.查看python有哪些版本使用命令 whereis python 如图: 2.这么多版本如何切换呢 使用 sudo update-alternatives --install <link&g ...