QTREE5 - Query on a tree V——LCT
动态点分治和动态边分治用Qtree4的做法即可。
LCT:
换根后,求子树最浅的白点深度。
但是也可以不换根。类似平常换根的往上g,往下f的拼凑
考虑深度的pushup必须考虑原树结构的联系,而ch[0],ch[1]又不是直接的前驱后继,每次pushup还要找前驱后继答案,还不如直接记下来。
故,节点里维护:
1.sz,大小
2.color节点颜色
3.set每个虚儿子贡献的最浅深度
4.lmn,rmn,当前x的splay子树最浅点和最深点的,展开成的实链的范围内的答案。
也就是:

pushup:

后面的就是跨过x的拼凑。top是虚儿子set的最小值
access:

虚实儿子转化
修改,access,splay,t[x].co^1
查询:access,splay, cout<<t[x].rmn
代码:
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{
const int N=+;
const int inf=0x3f3f3f3f;
int n,q;
struct node{
int ch[],fa;
int lmn,rmn;
int co;//1:white 0:black
int sz;
multiset<int>s;
multiset<int>::iterator it;
int top(){
if(s.size()) return *s.begin();
return inf;
}
void ins(int c){
s.insert(c);
}
void dele(int c){
it=s.lower_bound(c);
if(it!=s.end()) s.erase(it);
}
void op(){
cout<<" ch[0] "<<ch[]<<" ch[1] "<<ch[]<<" fa "<<fa<<endl;
cout<<" co "<<co<<" sz "<<sz<<endl;
cout<<" lmn "<<lmn<<" rmn "<<rmn<<endl;
cout<<" s.size() "<<s.size()<<endl;
}
}t[N];
int nrt(int x){
return t[t[x].fa].ch[]==x||t[t[x].fa].ch[]==x;
}
void pushup(int x){
if(!x) return;
t[x].sz=t[t[x].ch[]].sz+t[t[x].ch[]].sz+;
t[x].lmn=min(t[t[x].ch[]].lmn,t[t[x].ch[]].sz+min(t[x].co?:inf,min(t[x].top(),t[t[x].ch[]].lmn+)));
t[x].rmn=min(t[t[x].ch[]].rmn,t[t[x].ch[]].sz+min(t[x].co?:inf,min(t[x].top(),t[t[x].ch[]].rmn+)));
}
void rotate(int x){
int y=t[x].fa,d=t[y].ch[]==x;
t[t[y].ch[d]=t[x].ch[!d]].fa=y;
if(nrt(y)) t[t[x].fa=t[y].fa].ch[t[t[y].fa].ch[]==y]=x;
else t[x].fa=t[y].fa;
t[t[x].ch[!d]=y].fa=x;
pushup(y);
}
void splay(int x){
int y,z;
while(nrt(x)){
y=t[x].fa,z=t[y].fa;
if(nrt(y)){
rotate((t[y].ch[]==x)==(t[z].ch[]==y)?y:x);
}
rotate(x);
}
pushup(x);
}
void access(int x){
// cout<<" access "<<x<<endl;
for(reg y=;x;y=x,x=t[x].fa){
splay(x);
if(y) t[x].dele(t[y].lmn+);
if(t[x].ch[]) t[x].ins(t[t[x].ch[]].lmn+);
t[x].ch[]=y;
pushup(x);
// cout<<" x "<<x<<" y "<<y<<endl;
// t[x].op();
}
}
struct edge{
int nxt,to;
}e[*N];
int hd[N],cnt;
void add(int x,int y){
e[++cnt].nxt=hd[x];
e[cnt].to=y;
hd[x]=cnt;
}
void dfs(int x,int fa){
t[x].sz=;
t[x].lmn=inf;t[x].rmn=inf;
t[x].co=;
for(reg i=hd[x];i;i=e[i].nxt){
int y=e[i].to;
if(y==fa) continue;
t[y].fa=x;
dfs(y,x);
}
}
int main(){
rd(n);
int x,y;
for(reg i=;i<n;++i){
rd(x);rd(y);
add(x,y);add(y,x);
}
dfs(,);
rd(q);
int op;
t[].lmn=inf,t[].sz=;
t[].rmn=inf;t[].co=;
while(q--){
rd(op);rd(x);
if(!op){
access(x);splay(x);
t[x].co^=;
pushup(x);
// t[x].op();
}else{
access(x);splay(x);
// t[x].op();
printf("%d\n",t[x].rmn<=n?t[x].rmn:-);
}
}
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
Date: 2019/3/14 21:49:23
*/
见着拆招,pushup不好处理,就直接记录lmn和rmn,lmn用于access更新set,rmn用于查询答案。
如果要换根,还要考虑swap(lmn,rmn)
QTREE5 - Query on a tree V——LCT的更多相关文章
- QTREE5 - Query on a tree V(LCT)
题意翻译 你被给定一棵n个点的树,点从1到n编号.每个点可能有两种颜色:黑或白.我们定义dist(a,b)为点a至点b路径上的边个数. 一开始所有的点都是黑色的. 要求作以下操作: 0 i 将点i的颜 ...
- SPOJ - QTREE5 Query on a tree V 边分治
题目传送门 题意:给你一棵树, 然后树上的点都有颜色,且原来为黑,现在有2个操作,1 改变某个点的颜色, 2 询问树上的白点到u点的最短距离是多少. 题解: 这里用的还是边分治的方法. 把所有东西都抠 ...
- SPOJ QTREE Query on a tree V
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...
- SPOJ Query on a tree V
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...
- SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块
\(\color{#0066ff}{ 题目描述 }\) 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我们看: 0 u:询问有多少个节点v ...
- 2019.02.17 spoj Query on a tree V(链分治)
传送门 题意简述: 给你一棵nnn个黑白点的树,初始全是黑点. 现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离. 思路: 考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于 ...
- [spojQTREE5]Query on a tree V
合理的正解大概是动态点分治,这里给出其实现 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 100005 4 st ...
- SPOJ QTREE Query on a tree V ——动态点分治
[题目分析] QTREE4的弱化版本 建立出分治树,每个节点的堆表示到改点的最近白点距离. 然后分治树上一直向上,取min即可. 正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补. ...
- spoj 375 query on a tree LCT
这道题是树链剖分的裸题,正在学LCT,用LCT写了,发现LCT代码比树链剖分还短点(但我的LCT跑极限数据用的时间大概是kuangbin大神的树链剖分的1.6倍,所以在spoj上是850ms卡过的). ...
随机推荐
- php7函数,声明,返回值等新特性介绍
使用 ... 运算符定义变长参数函数 (PHP 5 >= 5.6.0, PHP 7) 现在可以不依赖 func_get_args(), 使用 ... 运算符 来实现 变长参数函数. functi ...
- 区分Python中的可变对象和不可变对象
参考: https://www.cnblogs.com/sun-haiyu/p/7096918.html """不过注意函数传参既不是传值也不是传引用,正确的叫法是传对象 ...
- Storm原理
zookeeper是对称结构
- vue bug & data type bug
vue bug & data type bug [Vue warn]: Invalid prop: type check failed for prop "value". ...
- npm安裝、卸載、刪除、撤銷發佈包、更新版本信息
利用npm安裝包: 全局安裝:npm install -g 模塊安裝 局部安裝(可以使用repuire(‘模塊名’)引用):npm install 模塊名稱 如果權限不夠,就是用管理員方式安裝. 本地 ...
- 三、checkedListBoxControl
一.checkedListBoxControl的使用全选 private void InitDate() { CheckedListBoxItem[] itemArr = { new CheckedL ...
- Lambda表达式Contains方法(等价于SQL语句中的like)使用注意事项
貌似已经半年多没写一篇帖子了,充分的说明要么老总一天折腾的让人心齐疲惫,没心情去写:要么另外一种可能就是自己不思进取,说白了就是懒.好在这种状态在今天被打破了.MoNey加油. 众所周知,想在Enti ...
- captive portal
刷好lineageos后默认浏览器无法上网,实际上并不是没有连上网,而是captive portal即网关设置错误,设置一下即可上网. adb shell "settings put glo ...
- ASP.NET Core 2.0 Cookie Authentication
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microso ...
- 思路:当一个表嵌套另一个表时候 只需在dao中引入该mapper即可 进行正常的数据插入 查询 修改等