codeforces 343d
题意:一棵树结构上有水,往一个节点加水,那么所有的子节点都会有水,或者排干一个节点的水,那么它的上面的节点都会没水。
用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的更多相关文章
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- CodeForces - 343D 树链剖分
题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- 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 ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- codeforces 343D 树剖后odt维护
子树修改+路径修改+单点查询 树链剖分+区间维护即可 由于只有单点查询,我直接用了odt,复杂度还行 #include<bits/stdc++.h> #define endl '\n' # ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Codeforces 343D Water Tree
题意简述 维护一棵树,支持以下操作: 0 v:将以v为跟的子树赋值为1 1 v:将v到根节点的路径赋值为0 2 v:询问v的值 题解思路 树剖+珂朵莉树 代码 #include <set> ...
随机推荐
- torch.utils.data.DataLoader使用方法
数据加载器,结合了数据集和取样器,并且可以提供多个线程处理数据集.在训练模型时使用到此函数,用来把训练数据分成多个小组,此函数每次抛出一组数据.直至把所有的数据都抛出.就是做一个数据的初始化. 生成迭 ...
- 最全的MonkeyRunner自动化测试从入门到精通(10)
三.MonkeyRunner复杂的功能开始学习 (1)获取APK文件中ID的两种方式 Monkeyrunner的环境已经搭建完成,现在对Monkeyrunner做一个简介. Monkeyrunner工 ...
- python摸爬滚打之day19----类的约束, 异常处理
1.类的约束 父类对子类某些功能的约束. python 中的两种约束: 1, 提取父类, 然后在父类中定义好方法, 该方法什么都不用干, 就通过主动抛出异常 raise NotImplementedE ...
- (转)git 忽略规则
对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是".gitignor ...
- [py]js前端求和与flask后端求和
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- Python四线程爬取西刺代理
import requests from bs4 import BeautifulSoup import lxml import telnetlib #验证代理的可用性 import pymysql. ...
- 【JVM】-NO.110.JVM.1 -【GC垃圾收集器】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- linux系统644、755、777权限详解
在linux系统中644.755.777三种权限是非常重要的一些权限了,下面我来详细的介绍644.755.777三种权限的使用,希望对各位有帮助. 常用的linux文件权限:444 r--r--r-- ...
- HDU 3033 分组背包(至少选一个)
分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...
- sublime----------快捷键的记录
1.鼠标选中多行,按下 Ctrl Shift L (Command Shift L) 即可同时编辑这些行: 2.鼠标选中自定义的多行,ctrl+鼠标左键