Codeforces Round #200 (Div. 1) D:http://codeforces.com/problemset/problem/343/D

题意:给你一颗有根树,树的每个节点有一个装水的东西,然后付亲的水可以流向儿子。一开始树上每个节点都是空的,然后会有两种操作。1 v,表示把v节点为子树的每个点都灌满水,2 v,表示v及v的祖先都清空。3 v是询问 v节点是否为空,为空输出0,否则输出1.

题解:首先,树上操作可以通过DFS,然后转化成数组操作。3操作来说,只要v的子树中有一个0,那么v一定是0,所以把单点查询,转化成了区间查询。2操作来说,我们可以只进行单点更新,即只对v这个节点操作。1操作的时候,就是区间更新,但是这里首先要查询v中是否有0,因为2操作只到了点。加入说,之前 2 v,那么v及其父亲应该是0的,这时候如果1,v的话,那么必须要把v的父亲变为0.这里有两种实现。先说一种好写。set运用。我们一开始把树上的每个节点都放进set,表示每个几点都是0.对于操作1.来说,首先我们要查询是否有0,Q.lower_bound(ll[temp]),如果找到并且这个数的位置不超过rr[temp],那么v的父亲要变成0,即可v的父亲查到set中,同时把那些变成1的元素从set中删除,Q.erase(Q.lower_bound(ll[temp]),Q.upper_bound(rr[temp]));3操作类似。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
const int N=5e5+;
int head[N],now;
int fa[N],ll[N],rr[N],cnt;
set<int>Q;
void init(){
memset(head,-,sizeof(head));
memset(rr,,sizeof(rr));
memset(ll,,sizeof(ll));
memset(fa,,sizeof(fa));
now=;
cnt=;
}
struct Node{
int v;
int next;
}edge[N*];
void add(int u,int v){
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void DFS(int u,int f){
ll[u]=rr[u]=++now;
Q.insert(now);
fa[u]=f;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v!=f)
DFS(v,u);
}
rr[u]=now;
}
int n,m,t1,t2;
int main(){
while(~scanf("%d",&n)){
init();
for(int i=;i<n;i++){
scanf("%d%d",&t1,&t2);
add(t1,t2);add(t2,t1);
}
DFS(,);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&t1,&t2);
if(t1==){
bool flag=false;
set<int>::iterator it=Q.lower_bound(ll[t2]);
if(it==Q.end()||(*it)>rr[t2])
flag=true;
Q.erase(Q.lower_bound(ll[t2]),Q.upper_bound(rr[t2]));
if(!flag&&t2!=)
Q.insert(ll[fa[t2]]);
}
else if(t1==){
Q.insert(ll[t2]);
}
else {
set<int>::iterator it=Q.lower_bound(ll[t2]);
if(it==Q.end()||(*it)>rr[t2])
printf("1\n");
else
printf("0\n");
}
}
}
}

第二种实现:线段树版本。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
const int N=5e5+;
int head[N],now;
int fa[N],ll[N],rr[N],cnt;
int n,m,t1,t2;
int lazy[N*],seg[N*];
void init(){
memset(head,-,sizeof(head));
memset(rr,,sizeof(rr));
memset(ll,,sizeof(ll));
memset(fa,,sizeof(fa));
memset(lazy,,sizeof(lazy));
memset(seg,,sizeof(seg));
now=;
cnt=;
}
struct Node{
int v;
int next;
}edge[N*];
void add(int u,int v){
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void DFS(int u,int f){
ll[u]=rr[u]=++now;
fa[u]=f;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v!=f)
DFS(v,u);
}
rr[u]=now;
}
void push_up(int rt){
seg[rt]=min(seg[rt<<],seg[rt<<|]);
}
void pushdown(int rt){
if(lazy[rt]>){
lazy[rt<<]=lazy[rt<<|]=lazy[rt];
seg[rt<<]=seg[rt<<|]=seg[rt];
lazy[rt]=-;
}
}
void update(int l,int r,int rt,int x,int y){
if(l==x&&y==r){
lazy[rt]=seg[rt]=;
return;
}
pushdown(rt);
int mid=(l+r)/;
if(mid>=y)update(l,mid,rt<<,x,y);
else if(mid<x)update(mid+,r,rt<<|,x,y);
else {
update(l,mid,rt<<,x,mid);
update(mid+,r,rt<<|,mid+,y);
}
push_up(rt);
}
void update1(int l,int r,int rt,int x){
if(l==r){
lazy[rt]=seg[rt]=;
return;
}
pushdown(rt);
int mid=(l+r)/;
if(mid>=x)update1(l,mid,rt<<,x);
else update1(mid+,r,rt<<|,x);
push_up(rt);
}
int query(int l,int r,int rt,int x,int y){
if(l==x&&r==y){
return seg[rt];
}
pushdown(rt);
int mid=(l+r)/;
if(mid>=y)return query(l,mid,rt<<,x,y);
else if(mid<x) return query(mid+,r,rt<<|,x,y);
else {
return min(query(l,mid,rt<<,x,mid),query(mid+,r,rt<<|,mid+,y));
}
} int main(){
while(~scanf("%d",&n)){
init();
for(int i=;i<n;i++){
scanf("%d%d",&t1,&t2);
add(t1,t2);add(t2,t1);
}
DFS(,);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&t1,&t2);
if(t1==){
int temp=query(,now,,ll[t2],rr[t2]);
update(,now,,ll[t2],rr[t2]);
if(temp==&&t2!=)
update1(,now,,ll[fa[t2]]);
}
else if(t1==){
update1(,now,,ll[t2]);
}
else {
printf("%d\n",query(,now,,ll[t2],rr[t2]));
}
}
}
}

Water Tree的更多相关文章

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

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

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

  3. 【题解】Luogu CF343D Water Tree

    原题传送门:CF343D Water Tree 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 这明显是弱智题 树剖套珂朵莉树多简单啊 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自 ...

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

  5. Water Tree(树链剖分+dfs时间戳)

    Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...

  6. xtu summer individual 6 F - Water Tree

    Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

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

  8. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  9. D. Water Tree

    D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  10. Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

随机推荐

  1. oracle12 pl/sql

    pl/sql块介绍 介绍   块(block)是pl/sql的基本程序单元,编写pl/sql程序实际上就是编写pl/sql块,要完成相对简单的应用功能,可能只需要编写一个pl/sql块,但是如果想要实 ...

  2. 8 Pratical Examples of Linux “Touch” Command--reference

    In Linux every single file is associated with timestamps, and every file stores the information of l ...

  3. 一个类搞定UIScrollView那些事

    前言 UIScrollView可以说是我们在日常编程中使用频率最多.扩展性最好的一个类,根据不同的需求和设计,我们都能玩出花来,当然有一些需求是大部分应用通用的,今天就聊一下以下需求,在一个categ ...

  4. html学习的一些问题

    1,什么是 W3C标准?w3c 标准不是一个标准,而是一系列标准,包括:结构标准,表现标准,动作标准. 2,内链元素和块状元素的区别内链元素允许与其他内链元素位于同一行,没有宽和高,如果想设置宽和搞, ...

  5. [Mime] MediaTypes--电子邮件类型类 (转载)

    点击下载 MediaTypes.rar 这个类是关于 电子邮件类型类的操作,在发送电子邮件是规定以什么样的格式发送,Xml,HTML,文本等方式1.电子邮件类型帮助类,Xml格式,HTML格式等看下面 ...

  6. java csv - 读写及其操作.

    今天帮同学处理数据, 主要是从1w多条记录中随机获取8k条, 然后再从8k条记录中随机获取2k条记录. 最后将2k条记录中随机分成10组,使得每组的记录都不重复. 下面将我的代码都贴上来, 好以后处理 ...

  7. Linux中一些目录名称的含义

    挖Linux中的古老缩略语[2005-06-22 15:23][Nigel McFarlane][TechTarget] Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世纪是相对于计算机 ...

  8. Sql Server内置函数实现MD5加密

    实例 MD5加密“123456”: HashBytes('MD5','123456') 结果:0xE10ADC3949BA59ABBE56E057F20F883E (提示:看完最后,结果要进行转换.) ...

  9. C++专题 - 面向对象总结

    1.         什么是类?什么是对象?对象与类的关系是什么? 答:类就是相同的数据和相同的一组对象的集合,即类是对具有相同数据结构和相同操作的一类对象的描述: 对象是描述其属性的数据以及对这些数 ...

  10. AngularJS 路由:ui-router

    UI-Router是Angular-UI提供的客户端路由框架,它解决了原生的ng-route的很多不足:视图不能嵌套.这意味着$scope会发生不必要的重新载入.这也是我们在Onboard中引入ui- ...