题意:一棵树结构上有水,往一个节点加水,那么所有的子节点都会有水,或者排干一个节点的水,那么它的上面的节点都会没水。

用dfs序,数组记录区间内全部有水为1,区间内有没水的点就为0。

倒水:区间更新,排水:单点更新,并更新途中经过的所有点,查询:区间查询。

倒水:区间内所有的点变为有水,就是1,用lazy数组,要下传。

排水:所有节点的dfs序之间的相交只能是包含,并且只被祖先包含。那么在线段树中就将经过的点全部排去。这样祖先节点查询时就为0了,并且无关节点还是有水。不过这样有个BUG,假如开始全有水,如果一个节点排空了立即加水,那么祖先又有水了,实际上是没水,这时将祖先排水就行。

6
2 1
3 1
4 1
4 5
2 6
4
1 1
2 4
1 4
3 1就这。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <unordered_set>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=*SZ,APB=,INF=0x7FFFFFFF,mod=;
lon n,bg[SZ],fn[SZ],cnt,arr[SSZ],lazy[SSZ];
lon pre[SZ];
vector<int> mp[SZ]; void release()
{ } void dfs(int x,int p)
{
bg[x]=++cnt;
pre[x]=p;
for(int i=;i<mp[x].size();++i)
{
int to=mp[x][i];
if(to!=p)
{
dfs(to,x);
}
}
fn[x]=cnt;
} void pushdown(int rt)
{
if(lazy[rt])
{
lazy[rt*]=lazy[rt*+]=;
arr[rt*]=arr[rt*+]=;
lazy[rt]=;
}
} void pushup(int rt)
{
arr[rt]=arr[rt*]*arr[rt*+];
} void update(int ll,int rr,int rt,int ql,int qr)
{
int mid=(ll+rr)/;
pushdown(rt);
if(ql<=ll&&rr<=qr)
{
arr[rt]=;
lazy[rt]=;
return;
}
if(ll>rr)return;
if(ql<=mid)update(ll,mid,rt*,ql,qr);
if(qr>mid)update(mid+,rr,rt*+,ql,qr);
pushup(rt);
} void update(int ll,int rr,int rt,int pos)
{
int mid=(ll+rr)/;
pushdown(rt);
if(ll==rr)
{
arr[rt]=;
return;
}
if(pos<=mid)update(ll,mid,rt*,pos);
else update(mid+,rr,rt*+,pos);
pushup(rt);
} int qry(int ll,int rr,int rt,int ql,int qr)
{
//cout<<" "<<ll<<" "<<rr<<endl;
int mid=(ll+rr)/;
pushdown(rt);
if(ql<=ll&&rr<=qr)
{
return arr[rt];
}
if(ll>rr)return ;
int res1=,res2=;
if(ql<=mid)res1=qry(ll,mid,rt*,ql,qr);
if(qr>mid)res2=qry(mid+,rr,rt*+,ql,qr);
return res1*res2;
} void init()
{
cin>>n;
for(int i=;i<=n-;++i)
{
int a,b;
cin>>a>>b;
mp[a].push_back(b);
mp[b].push_back(a);
}
dfs(,-);
} void work()
{
int qnum;
cin>>qnum;
for(int i=;i<=qnum;++i)
{
int type,x;
cin>>type>>x;
if(type==)
{
if(x!=&&!qry(,cnt,,bg[x],fn[x]))update(,cnt,,bg[pre[x]]);
update(,cnt,,bg[x],fn[x]);
}
else if(type==)
{
update(,cnt,,bg[x]);
}
else
{
//cout<<"3: "<<bg[x]<<" "<<fn[x]<<endl;
cout<<qry(,cnt,,bg[x],fn[x])<<endl;
}
}
} int main()
{
//std::ios::sync_with_stdio(0);
//freopen("d:\\2.txt","r",stdin);
//freopen("d:\\3.txt","w",stdout);
lon casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(lon time=1;time<=casenum;++time)
//for(lon time=1;scanf("%d%d",&n,&m)!=EOF;++time)
{
init();
work();
release();
}
return ;
}

codeforces 343d的更多相关文章

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

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

  2. CodeForces - 343D 树链剖分

    题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...

  3. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

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

  5. CodeForces 343D 线段树维护dfs序

    给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...

  6. Codeforces 343D WaterTree - 线段树, DFS序

    Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...

  7. codeforces 343D 树剖后odt维护

    子树修改+路径修改+单点查询 树链剖分+区间维护即可 由于只有单点查询,我直接用了odt,复杂度还行 #include<bits/stdc++.h> #define endl '\n' # ...

  8. CodeForces 343D water tree(树链剖分)

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

  9. Codeforces 343D Water Tree

    题意简述 维护一棵树,支持以下操作: 0 v:将以v为跟的子树赋值为1 1 v:将v到根节点的路径赋值为0 2 v:询问v的值 题解思路 树剖+珂朵莉树 代码 #include <set> ...

随机推荐

  1. linux命令:压缩解压打包工具大集合

    目录 (1)zip 压缩.解压缩及归档工具有很多,今天小编就整理几个大家较为常用的. compress gzip  bzip2 xz zip tar cpio 一.压缩.解压工具 用法 压缩 工具 压 ...

  2. 关于mysql中like查询是否通过索引的测试

    测试mysql的like语句是否通过索引时得到结果如下: 图片1: 图片2: 图片3: 通过上述3组图片我想大家很容易愤青我使用的'%8888888%','%8888888'和'8888888%'3中 ...

  3. JAVA spring 常用包作用详解(转)

    转载地址:https://www.cnblogs.com/Tmc-Blog/p/6093162.html <project xmlns="http://maven.apache.org ...

  4. java并发包消息队列(也即阻塞队列BlockingQueue)

    下面是典型的消息队列的生产者与消费者模式的例子

  5. Python 总结

    python3.7下载地址 Python安装pip 1.首先检查linux有没有安装python-pip包,直接执行 yum install python-pip 2.没有python-pip包就执行 ...

  6. 一个不错的git资源站点

    https://backlog.com/git-tutorial/cn/stepup/stepup2_8.html

  7. 设置vim支持gbk

    linux下的默认字符集是utf-8,但Windows下默认是GBK,如果我们在linux下打开Windows中的文件就很容乱码,可以通过下面的设置使vim支持GBK编码. 首先,确认你的系统中安装了 ...

  8. 安装OpenResty开发环境

    OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 Web ...

  9. Web_Toy

    1 2 3 4 1.App录音 var r = plus.audio.getRecorder() # 创建录音对象 r.record({filename:"_doc/audio/" ...

  10. 校验input 修改当前值的校验获取值方式

    <input style="height: 18px; width: 90px;" name="nowQty" value="${ad.nowQ ...