2019.02.17 spoj Query on a tree V(链分治)
传送门
题意简述:
给你一棵nnn个黑白点的树,初始全是黑点。
现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离。
思路:
考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于每条重链开一棵线段树维护子树里所有白点到线段树最左/右端点的最短距离。
然后瞎更新查询即可。
代码:
#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<<2)+ans)<<1)+(ch^48),ch=getchar();
return ans;
}
const int N=1e5+5,inf=1e8;
bool col[N];
struct deletable_priority_queue{
priority_queue<int,vector<int>,greater<int> >a,b;
inline void push(const int&x){a.push(x);}
inline void del(const int&x){b.push(x);}
inline int top(){while(b.size()&&a.top()==b.top())a.pop(),b.pop();return a.top();}
inline int size(){return a.size()-b.size();}
inline void f(int x,int&a,int&b){
a=b=inf;
if(col[x])a=b=0;
if(size())a=b=min(a,top());
}
}h[N];
int siz[N],hson[N],top[N],num[N],bot[N],pred[N],fa[N],n,m,tot=0,dep[N];
vector<int>e[N];
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;
fa[v]=p,dep[v]=dep[p]+1,dfs1(v),siz[p]+=siz[v];
if(siz[v]>siz[hson[p]])hson[p]=v;
}
}
void dfs2(int p,int tp){
top[p]=tp,bot[tp]=p,pred[num[p]=++tot]=p;
if(!hson[p])return;
dfs2(hson[p],tp);
for(ri i=0,v;i<e[p].size();++i){
if((v=e[p][i])==hson[p]||v==fa[p])continue;
dfs2(v,v);
}
}
namespace SGT{
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
struct Node{int l,r,ls,rs,sum;}T[N<<2];
inline Node operator+(const Node&a,const Node&b){
Node ret;
ret.l=a.l,ret.r=b.r;
ret.sum=a.sum+1+b.sum;
ret.ls=min(a.ls,a.sum+1+b.ls);
ret.rs=min(b.rs,b.sum+1+a.rs);
return ret;
}
inline void Set(int p){
int k=pred[T[p].l];
T[p].sum=0,h[k].f(k,T[p].ls,T[p].rs);
}
inline void pushup(int p){T[p]=T[lc]+T[rc];}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r,T[p].ls=T[p].rs=inf,T[p].sum=0;
if(l==r)return;
build(lc,l,mid),build(rc,mid+1,r);
}
inline void update(int p,int k){
if(T[p].l==T[p].r)return Set(p);
update(k<=mid?lc:rc,k),pushup(p);
}
inline Node query(int p,int ql,int qr){
if(ql<=T[p].l&&T[p].r<=qr)return T[p];
if(qr<=mid)return query(lc,ql,qr);
if(ql>mid)return query(rc,ql,qr);
return query(lc,ql,qr)+query(rc,ql,qr);
}
#undef lc
#undef rc
#undef mid
}
int dfs3(int tp){
for(ri p=tp;p;p=hson[p]){
for(ri i=0,v;i<e[p].size();++i){
if((v=e[p][i])==fa[p]||v==hson[p])continue;
h[p].push(dfs3(v)+1);
}
SGT::update(1,num[p]);
}
return SGT::query(1,num[tp],num[bot[tp]]).ls;
}
inline void update(int p){
while(p){
int ft=fa[top[p]],tp=top[p];
if(ft){
SGT::Node tmp=SGT::query(1,num[tp],num[bot[tp]]);
h[ft].del(tmp.ls+1);
}
SGT::update(1,num[p]);
if(ft){
SGT::Node tmp=SGT::query(1,num[tp],num[bot[tp]]);
h[ft].push(tmp.ls+1);
}
p=ft;
}
}
inline int query(int x){
int p=x,ret=SGT::query(1,num[x],num[bot[top[x]]]).ls;
while(p){
int ft=fa[top[p]],tp=top[p];
ret=min(ret,SGT::query(1,num[tp],num[p]).rs+dep[x]-dep[p]);
ret=min(ret,SGT::query(1,num[p],num[bot[tp]]).ls+dep[x]-dep[p]);
p=ft;
}
return ret;
}
int main(){
n=read();
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),SGT::build(1,1,n),dfs3(1);
for(ri all=0,v,tt=read();tt;--tt){
if(read()){
v=read();
if(!all)puts("-1");
else if(col[v])puts("0");
else cout<<query(v)<<'\n';
}
else{
v=read();
col[v]^=1;
if(col[v])++all;
else --all;
update(v);
}
}
return 0;
}
2019.02.17 spoj Query on a tree V(链分治)的更多相关文章
- 2019.02.17 spoj Query on a tree VII(链分治)
传送门 跟QTREE6QTREE6QTREE6神似,改成了求连通块里的最大值. 于是我们对每条链开一个heapheapheap维护一下即可. MDMDMD终于1A1A1A链分治了. 代码: #incl ...
- 2019.02.17 spoj Query on a tree VI(链分治)
传送门 题意简述:给你一棵nnn个黑白点的树,支持改一个点的颜色,询问跟某个点颜色相同的连通块大小. 思路: 还是链分治 233 记fi,0/1f_{i,0/1}fi,0/1表示iii的所有颜色为0 ...
- 2019.02.16 spoj Query on a tree IV(链分治)
传送门 题意简述: 捉迷藏强化版(带有边权,可以为负数) 思路:好吧这次我们不用点分树,我们用听起来更屌的链分治. 直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- SPOJ Query on a tree V
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...
- SPOJ - QTREE5 Query on a tree V 边分治
题目传送门 题意:给你一棵树, 然后树上的点都有颜色,且原来为黑,现在有2个操作,1 改变某个点的颜色, 2 询问树上的白点到u点的最短距离是多少. 题解: 这里用的还是边分治的方法. 把所有东西都抠 ...
- QTREE5 - Query on a tree V——LCT
QTREE5 - Query on a tree V 动态点分治和动态边分治用Qtree4的做法即可. LCT: 换根后,求子树最浅的白点深度. 但是也可以不换根.类似平常换根的往上g,往下f的拼凑 ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- SPOJ QTREE Query on a tree 树链剖分+线段树
题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...
随机推荐
- 谷歌浏览器内核Cef js代码整理(一)
尊重作者原创,未经作者允许不得转载!作者:xtfnpgy,原文地址: https://www.cnblogs.com/xtfnpgy/p/9285359.html 一.js基础知识 <!-- ...
- django 分页和中间件
分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...
- linux服务nfs与dhcp篇
nfs复习: 1.简介:用于liunx与linux之间的文件传输系统 2.下载nfs-utils和rpcbind 3.打开配置文件/etc/exports——文件名(目录名)共享给予的ip地址(rw) ...
- dubbo常用配置及注意事项
1.启动时检查缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check=true. 关闭所有服务的启动时检查:(没有提供者时报错 ...
- spring boot 接口用例测试
接口: 测试用例:
- 多个css样式合并到一个“目录”css文件中
执行访问jsp后发现没有效果 同样的代码,在html中效果对比如下: 具体原因:不清楚,暂时记着~~~在jsp中不支持@import这种css样式的引用
- BUILDING WITH BOOTSTRAP
BUILDING WITH BOOTSTRAP Bootstrap Generalizations You just built an impressive webpage using the Boo ...
- java.lang.String (JDK1.8)
String类实现了java.io.Serializable, Comparable<String>, CharSequence这三个interface. 看了下这三个interface中 ...
- week07 13.4 NewsPipeline之 三 News Deduper
还是循环将Q2中的东西拿出来 然后查重(去mongodb里面把一天之内的新闻都拿出来,然后把拿到的新的新闻和mongodb里一天内的新闻组一个 tf-idf的对比)可看13.3 相似度检查 如果超过一 ...
- Python中的logging模块【转】https://www.cnblogs.com/yelin/p/6600325.html
[转]https://www.cnblogs.com/yelin/p/6600325.html 基本用法 下面的代码展示了logging最基本的用法. 1 # -*- coding: utf-8 -* ...