QTREE5 - Query on a tree V

动态点分治和动态边分治用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的更多相关文章

  1. QTREE5 - Query on a tree V(LCT)

    题意翻译 你被给定一棵n个点的树,点从1到n编号.每个点可能有两种颜色:黑或白.我们定义dist(a,b)为点a至点b路径上的边个数. 一开始所有的点都是黑色的. 要求作以下操作: 0 i 将点i的颜 ...

  2. SPOJ - QTREE5 Query on a tree V 边分治

    题目传送门 题意:给你一棵树, 然后树上的点都有颜色,且原来为黑,现在有2个操作,1 改变某个点的颜色, 2 询问树上的白点到u点的最短距离是多少. 题解: 这里用的还是边分治的方法. 把所有东西都抠 ...

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

  4. SPOJ Query on a tree V

    You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...

  5. SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块

    \(\color{#0066ff}{ 题目描述 }\) 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我们看: 0 u:询问有多少个节点v ...

  6. 2019.02.17 spoj Query on a tree V(链分治)

    传送门 题意简述: 给你一棵nnn个黑白点的树,初始全是黑点. 现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离. 思路: 考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于 ...

  7. [spojQTREE5]Query on a tree V

    合理的正解大概是动态点分治,这里给出其实现 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 100005 4 st ...

  8. SPOJ QTREE Query on a tree V ——动态点分治

    [题目分析] QTREE4的弱化版本 建立出分治树,每个节点的堆表示到改点的最近白点距离. 然后分治树上一直向上,取min即可. 正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补. ...

  9. spoj 375 query on a tree LCT

    这道题是树链剖分的裸题,正在学LCT,用LCT写了,发现LCT代码比树链剖分还短点(但我的LCT跑极限数据用的时间大概是kuangbin大神的树链剖分的1.6倍,所以在spoj上是850ms卡过的). ...

随机推荐

  1. 【学亮开讲】Oracle存储过程教学笔记(二)20181116

    --带出参的存储过程的创建和调用 create or replace procedure pro_owners_add1 ( v_name varchar2,--名称 v_addressid numb ...

  2. Linux基础操作二

    编程语言的作用及与操作系统和硬件的关系 编程语言的作用:用来定义计算机程序的形式,程序员用它来编写程序,进而控制其向计算机发出指令,使计算机完成人类布置的任务. 编程语言的作用及与操作系统和硬件的关系 ...

  3. kubernetes常用命令

    #.查询信息 kubectl get [需要查询的服务]   node 节点componentstatuses 简写 cs 组件状态namespaces 简写 ns 名命空间pod pod信息 添加  ...

  4. 如何使用Action.Invoke()触发一个Storyboard

    一般在我们的项目中,最好是将Storyboard放在前台,然后设置Storyboard的x:key值,通过我们的TryFindResource来查找到当前的Storyboard来启动Stroyboar ...

  5. Spring标签之Bean @Scope

    @Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...

  6. python数据结构与算法第六天【栈与队列】

    1.栈和队列的原理 栈:后进先出(LIFO),可以使用顺序表和链表实现 队列:先进先出(FIFO),可以使用顺序表和链表实现 2.栈的实现(使用顺序表实现) #!/usr/bin/env python ...

  7. Delphi处理数据网格DBGrid的编辑框 获取还没有提交到数据集的字段文本

    //fromhttp://kingron.myetang.com/zsfunc12.htm (*//标题:处理数据网格的编辑框说明:示例添加焦点颜色;获取还没有提交到数据集的字段文本设计:Zswang ...

  8. String 常见的十种方法!

    public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...

  9. ASP.NET MVC和Web API中的Angular2 - 第1部分

    下载源码 - 903.5 KB 内容 第1部分:Visual Studio 2017中的Angular2设置,基本CRUD应用程序,第三方模态弹出控件 第2部分:使用Angular2管道进行过滤/搜索 ...

  10. 使用Windows任务计划程序运行Windows PowerShell脚本

    创建计划任务以运行PowerShell脚本 我需要创建一个计划任务来运行Windows PowerShell脚本的第一件事是我将执行的命令行.找到这个的简单方法是使用Run 命令.有时,我需要知道什么 ...