2019.01.19 codeforces343D.Water Tree(树剖+ODT)
传送门
ODTODTODT板子题。
支持子树01覆盖,路径01覆盖,询问一个点的值。
思路:当然可以用树剖+线段树,不过树剖+ODTODTODT也可以很好的水过去。
注意修改路径时每次跳重链都要修改。
不会ODTODTODT的点这里
代码:
#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
const int N=5e5+5;
int top[N],hson[N],siz[N],dep[N],num[N],fa[N],tot=0,n;
vector<int>e[N];
struct Node{
int l,r;
mutable bool v;
Node(int l,int r=-1,bool v=0):l(l),r(r),v(v){}
friend inline bool operator<(const Node&a,const Node&b){return a.l<b.l;}
};
set<Node>S;
typedef set<Node>::iterator It;
inline It split(int pos){
It it=S.lower_bound(Node(pos));
if(it!=S.end()&&it->l==pos)return it;
--it;
int l=it->l,r=it->r;
bool v=it->v;
return S.erase(it),S.insert(Node(l,pos-1,v)),S.insert(Node(pos,r,v)).first;
}
inline void assign(int l,int r,bool v){
It R=split(r+1),L=split(l);
S.erase(L,R),S.insert((Node){l,r,v});
}
inline bool query(int pos){
It it=S.lower_bound(Node(pos));
if(it!=S.end()&&it->l==pos)return it->v;
return --it,it->v;
}
void dfs1(int p){
siz[p]=1;
for(ri i=0,v;i<e[p].size();++i){
if((v=e[p][i])==fa[p])continue;
dep[v]=dep[p]+1,fa[v]=p,dfs1(v),siz[p]+=siz[v];
if(siz[v]>siz[hson[p]])hson[p]=v;
}
}
void dfs2(int p,int tp){
top[p]=tp,num[p]=++tot;
if(!hson[p])return;
dfs2(hson[p],tp);
for(ri i=0,v;i<e[p].size();++i)if((v=e[p][i])!=fa[p]&&v!=hson[p])dfs2(v,v);
}
inline void change(int x){
while(top[x]^1)assign(num[top[x]],num[x],0),x=fa[top[x]];
assign(1,num[x],0);
}
int main(){
S.insert(Node(1,n=read(),0));
for(ri i=1,u,v;i<n;++i)u=read(),v=read(),e[u].push_back(v),e[v].push_back(u);
dfs1(1),dfs2(1,1);
for(ri tt=read(),op,x;tt;--tt){
op=read(),x=read();
switch(op){
case 1:assign(num[x],num[x]+siz[x]-1,1);break;
case 2:change(x);break;
default:cout<<query(num[x])<<'\n';
}
}
return 0;
}
2019.01.19 codeforces343D.Water Tree(树剖+ODT)的更多相关文章
- 2019.01.19 bzoj4592: [Shoi2015]脑洞治疗仪(ODT)
传送门 ODT水题. 支持区间01赋值,区间填补(把区间[l,r][l,r][l,r]从左往右数kkk个1都变成0),区间查询最长连续1个数. 思路: 区间填补操作感觉不是很好弄,写线段树的神仙可以套 ...
- 2019.01.19 codeforces915E.Physical Education Lessons(ODT)
传送门 ODT水题(当然可以上线段树) 支持区间01覆盖,询问全局1的个数. 思路:直接上ODTODTODT. 不会的点这里 代码: #include<bits/stdc++.h> #de ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- 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 ...
- Water Tree(树链剖分+dfs时间戳)
Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...
- CF343D Water Tree 树链剖分
问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...
- SP375 QTREE - Query on a tree (树剖)
题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...
- 【2019北京集训3】逻辑 树剖+2-sat
题目大意:有一颗有$m$个叶子节点的二叉树. 对于叶子节点$i$,$x[i]=(a[i]\ xor\ V_{p[i]})or(b[i]\ xor\ V_{q[i]})$ 对于非叶子节点$i$,$x[i ...
- CF 504 E —— Misha and LCP on Tree —— 树剖+后缀数组
题目:http://codeforces.com/contest/504/problem/E 快速查询LCP,可以用后缀数组,但树上的字符串不是一个序列: 所以考虑转化成序列—— dfs 序! 普通的 ...
随机推荐
- Shell教程 之变量
1.Shell变量 1.1 定义变量 your_name="http://www.cnblogs.com/uniquefu" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有 ...
- oracle中job定时任务96
.INTERVAL参数常用值示例 每天午夜12点 ''TRUNC(SYSDATE + 1)'' 每天早上8点30分 ''TRUNC(SYSDATE + 1) + ...
- 不包含数据和字母的Webshell
在做php20中的challenge5时学习到了php中可代替数字和字母的符号,悲剧的是这道题没做出来,目前先整理知识点吧555555555555 情况是:很多时候在敏感地方WAF会阻 ...
- nth-child & nth-of-type区别
Do l have to 非做不可吗? He is my age. 他和我同岁. Here you are. 给你. No one knows . 没有人知道. 关于nth-child &&a ...
- AtCoder Regular Contest 096 D - Static Sushi(线性dp)
Problem Statement "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only o ...
- Qt: 加入打印支持
写记事本打印功能所遇问题,记录以便于查阅. 在vs系统中开发Qt,加入打印支持,不似在QtCreator之pro文件中中加QT +=printsupport解决. 而要在vs(以vs2015为例)中, ...
- 《基于Nginx的中间件架构》学习笔记---1.环境配置
一.环境调试确认 (四项确认) 1.确认系统网络 ping www.baidu.com 2.确认yum可用 yum list|grep gcc 3.确认关闭iptables规则 iptables -L ...
- Android沉浸式状态栏背景色以及字体颜色的修改
在activity中设置透明状态栏 的思路: 1.让activity的布局全屏 此时布局会和状态栏重叠 2.让布局最上方预留出和状态栏高度一样的高度,将状态栏的背景色设置为透明 效 ...
- pthreads v3下的worker和pool的使用
有些人会想,明明用thread已经可以很好的工作了,为什么还要搞个worker和pool? 之所以要用到worker和pool还是因为效率,因为系统创建一个新线程代价是比较昂贵,每个创建的线程会复制当 ...
- golang语言中sync/atomic包的学习与使用
package main; import ( "sync/atomic" "fmt" "sync" ) //atomic包提供了底层的原子级 ...