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. CentOS7安装Cobbler

    安装EPEL源 # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 安装cobbler ...

  2. 【转】char码值对应列表大全

    char("56") A char("97") a [转]char码值对应列表大全 Char("0") 为0的字符Char("1& ...

  3. sql中对查询出来的数据进行分页

    当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...

  4. python基础知识十一

    图形软件 使用Python的GUI库——你需要使用这些库来用Python语言创建你自己的图形程序.使用GUI库和它们的Python绑定,你可以创建你自己的IrfanView.Kuickshow软件或者 ...

  5. Android - 服务器json数据交互.

    一,服务器端 服务器端使用的是Servlet,封装json对象使用的 'json-lib-2.2.2-jdk15.jar,ezmorph-1.0.4.jar,commons-logging-1.1.j ...

  6. 【HDU1402】【FNT版】A * B Problem Plus

    Problem Description Calculate A * B.   Input Each line will contain two integers A and B. Process to ...

  7. Strategy 模式

    可以看到 Strategy 模式和 Template 模式解决了类似的问题,也正如在 Template 模式中分析的,Strategy模式和 Template 模式实际是实现一个抽象接口的两种方式:继 ...

  8. who am i

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  9. Setup VSFTPD Server with Virtual Users On CentOS, RHEL, Scientific Linux 6.5/6.4/6.3

    We have already shown you How to Setup VSFTPD Server on CentOS 6.5/6.4 in our previous article. In t ...

  10. JavaScript学习总结【10】、DOM 事件

    DOM 事件是 JS 中比较重要的一部分知识,所谓事件,简单理解就是用户对浏览器进行的一个操作.事件在 Web 前端领域有很重要的地位,很多重要的知识点都与事件有关,所以学好 JS 事件可以让我们在J ...