Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
给出一棵树,有三种操作:
1 x:把以x为子树的节点全部置为1
2 x:把x以及他的所有祖先全部置为0
3 x:询问节点x的值
分析:
昨晚看完题,马上想到直接树链剖分,在记录时间戳时需要记录一下出去时的时间戳,然后就是很裸很裸的树链剖分了。
稳稳的黄名节奏,因为一点私事所以没做导致延迟了
(ps:后来想了一下,不用树链剖分直接dfs序维护也行。。。)
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ char op,s[12];
int tp;
inline void Int(int &x){
while( !isdigit(op=getchar()) );
x = op-'0';
while( isdigit(op=getchar()) )
x = x*10+op-'0';
}
inline void LL(ll &x){
while( !isdigit(op=getchar()) );
x = op-'0';
while( isdigit(op=getchar()) )
x = x*10+op-'0';
}
inline void Out(ll x){
s[0] = '0';
tp = 0;
while(x){
s[tp++] = x%10+'0';
x /= 10;
}
for(int i=tp-1;i>=0;i--)
putchar(s[i]);
puts("");
} const int MAXN = 500005; int son[MAXN],sz[MAXN],dep[MAXN],fa[MAXN],top[MAXN],tim;
bool use[MAXN];
int st[MAXN],ed[MAXN];
int po[MAXN],tol;
int n,m; struct Edge{
int y,next;
}edge[MAXN<<1]; inline void add(int x,int y){
edge[++tol].y = y;
edge[tol].next = po[x];
po[x] = tol;
} // 树链剖分
void dfsFind(int x,int pa,int depth){
son[x] = 0;
sz[x] = 1;
dep[x] = depth;
fa[x] = pa;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(y==pa)continue;
dfsFind(y,x,depth+1);
sz[x] += sz[y];
if(sz[y]>sz[son[x]])
son[x] = y;
}
} void dfsCon(int x,int pa){
use[x] = true;
top[x] = pa;
st[x] = ++ tim; // 记录进入时的时间戳
if(son[x])dfsCon(son[x],pa);
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(!use[y])dfsCon(y,y);
}
ed[x] = tim;
} // 线段树部分
struct segTree{
int l,r;
int col,lz;
inline int mid(){
return (l+r)>>1;
}
}tree[MAXN<<2]; inline void push(int rt){
if(tree[rt].lz){
tree[rt<<1].lz = tree[rt<<1|1].lz = 1;
tree[rt<<1].col = tree[rt<<1|1].col = tree[rt].col;
tree[rt].lz = 0;
}
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
tree[rt].lz = 0;
tree[rt].col = 0;
tree[rt].lz = 0;
if(l==r)return;
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
} void modify(int l,int r,bool col,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r){
tree[rt].lz = true;
tree[rt].col = col;
return;
}
push(rt);
int mid = tree[rt].mid();
if(r<=mid)modify(l,r,col,rt<<1);
else if(l>mid)modify(l,r,col,rt<<1|1);
else{
modify(l,r,col,rt<<1);
modify(l,r,col,rt<<1|1);
}
} int ask(int pos,int rt){
if(tree[rt].l==tree[rt].r)
return tree[rt].col;
push(rt);
int mid = tree[rt].mid();
if(pos<=mid)return ask(pos,rt<<1);
else return ask(pos,rt<<1|1);
} inline void lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
modify( st[top[x]],st[x],0,1 );
x = fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
modify(st[x],st[y],0,1);
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int n,m,x,y;
cin>>n;
REP(i,2,n){
RD2(x,y);
add(x,y);
add(y,x);
} dfsFind(1,1,1);
Clear(use);
tim = 0;
dfsCon(1,1); build(1,n,1); RD(m);
while(m--){
RD2(y,x);
if(y==1)
modify( st[x],ed[x],1,1 );
else if(y==2)
lca(1,x);
else
printf("%d\n",ask(st[x],1));
} return 0;
}
Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序的更多相关文章
- 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 ...
- Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...
- 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/ ...
- 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 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Water Tree(树链剖分+dfs时间戳)
Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
- CF343D Water Tree 树链剖分
问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...
随机推荐
- android UI开源库
. ActionBarSherlock ActionBarSherlock是一个独立的Android设计库,可以让Android 2.x的系统也能使用ActionBar.此 外,ActionBarSh ...
- WPF让人哭笑不得的资源
前几天遇到了一个让我哭笑不得的bug,我写的Wpf程序在Win7里可以运行,到XP.WindowsServer里运行点击某个控件之后闪退,不报任何错,在后台代码里trycatch也捕捉不到任何异常.很 ...
- until与till的用法归纳
until与till的用法归纳 崔荣斌 until和till两者都可作介词.连词,一般情况下可以互换使用.用于肯定句时,主句的动词只用延续性的,它所表示的动作一直延续到till或until表示的时间为 ...
- IE6下z-index失效
一.匆匆带过的概念关于CSS中层级z-index的定义啊什么的不是本文的重点,不会花费过多篇幅详细讲述.这里就简单带过,z-index伴随着层的概念产生的.网页 中,层的概念与photoshop或是f ...
- QML学习笔记之三
import QtQuick 1.1 Row{ spacing:2 Rectangle{color:"red";width:50;height:50} Rectangle{colo ...
- Codeforces Round #340 (Div. 2) A. Elephant 水题
A. Elephant 题目连接: http://www.codeforces.com/contest/617/problem/A Descriptionww.co An elephant decid ...
- 手把手让你爱上Android sdk自带“9妹”(9patch 工具)
前几天群成员讨论过关于9patch的工具[我比较喜欢喊它9妹子,西西(*^_^*)].然后研究了一下,比较简单但是很实用的一个Android sdk 自带工具.这里给大家做一个分享下经验! 1.什么是 ...
- AJAX responseText vs responseXML
AJAX------>本质 Request/Response(Server)模式 response的形式 responseText--------->获得字符串形式的响应数据. ----- ...
- Objective-C面向对象的编程
Objective-C面向对象的编程 目录 对面向对象编程思想的理解 类的声明和定义 类的声明和定义 对关键字super和self的理解 初始化函数 @property声明类成员 类的实例化 继承 组 ...
- CentOS 6.0 图形(图解)安装教程
http://www.cnblogs.com/vipsoft/archive/2012/04/23/2466062.html