spoj Query on a tree V(链分治)

传送门
题意简述:
给你一棵 n n n个黑白点的树,初始全是黑点。
现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离。


思路:
考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于每条重链开一棵线段树维护子树里所有白点到线段树最左/右端点的最短距离。
然后瞎更新查询即可。
代码:

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值