洛谷P4092 [HEOI2016/TJOI2016]树(树链剖分)

在这里插入图片描述
思路:显然需要一些树上的数据结构支持我们完成这些蛋疼的操作.
对于这种似乎是树上的问题,可以先试着解决数组的情况
如果这个题是一个数组,就是动态查询一个点最近被染色的前驱嘛.
我们会怎么解决呢?用一个线段树维护下这个区间 [ l , r ] [l,r] [l,r]中最后边被染色的点.
每次查询一个点,就去查询区间 [ 1 , x ] [1,x] [1,x]中最后一个被染色的点就可以了
现在,这个问题被移植到了树上,如果想在树上完成线段树的操作,那么这种数据结构已经呼之欲出了——树链剖分.
使用树链剖分来维护每个链内的最深的被染色的点,对应就是查询区间 [ r t , x ] [rt,x] [rt,x]这个间最深的一个被染色的点.
运用这种思想,小心翼翼地书写树链剖分,线段树部分只需要实现一个点修改,区间查询的功能.而树链剖分的部分,在查询的时候,如果我们在一个段里边找到了答案,就马上返回答案.因为这肯定是最近的那一个,否则就一路往上跳.因为根一定被标记了,不会出现没有解的情况.
代码:(本蒟蒻居然一次过,难见)

/*
You held me down but I broke free,
I found the love inside of me.
Now I don't need a hero to survive
Cause I already saved my life.
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 1e9+7;
typedef long long ll;
typedef pair<int,int> pii;
#define all(a) (a).begin(), (a).end()
#define pb(a) push_back(a)
vector<int> G[maxn];
struct Node{
	int x,y;//<depth,id>
}tree[maxn*4+5];
int tag[maxn*4+5];
#define L (idx<<1)
#define R (idx<<1|1)
#define MID (start+end>>1)
int n,m;
int son[maxn],fa[maxn],depth[maxn],top[maxn],sz[maxn],dfs_clock=0;
int id[maxn];
void push_up(int idx){
	if(tree[L].x>=tree[R].x){
		tree[idx] = tree[L];
	}
	else tree[idx] = tree[R];
}
void build(int idx,int start,int end){
	if(start==end){
		tree[idx].x = tree[idx].y = -1;
		return ;
	}
	build(L,start,MID);build(R,MID+1,end);
	push_up(idx);
}
void update(int idx,int start,int end,int pos,int val,int k){
	if(start==end&&start==pos){
		tree[idx].x = val;
		tree[idx].y = k;
		return ;
	}
	if(pos>=start&&pos<=MID) update(L,start,MID,pos,val,k);
	if(pos>=MID+1&&pos<=end) update(R,MID+1,end,pos,val,k);
	push_up(idx);
}
Node query(int idx,int start,int end,int l,int r){
	if(start>=l&&end<=r) return tree[idx];
	Node ans = {-1,-1};
	if(l<=MID){
		ans = query(L,start,MID,l,r);
	}
	if(r>MID){
		Node tmp = query(R,MID+1,end,l,r);
		if(tmp.x>ans.x) ans = tmp;
	}
	return ans;
}
void dfs1(int u,int f){
	depth[u] = depth[f]+1;fa[u] =f;
	sz[u] =1;
	for(auto v : G[u]){
		if(v==f) continue;
		dfs1(v,u);sz[u]+=sz[v];
		if(son[u]==0||sz[son[u]]<sz[v]) son[u] = v;
	}
}
void dfs2(int u,int topf){
	id[u] = ++dfs_clock;top[u] = topf;
	if(son[u]) dfs2(son[u],topf);
	for(auto v : G[u]){
		if(v==fa[u]||v==son[u]) continue;
		dfs2(v,v);
	}
}
int tr_query(int x,int y){
	Node ans = {-1,-1};
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]]) swap(x,y);
		ans = query(1,1,n,id[top[x]],id[x]);
		if(ans.x!=-1) return ans.y;
		x = fa[top[x]];
	}
	if(depth[x]>depth[y]) swap(x,y);
	ans = query(1,1,n,id[x],id[y]);
	return ans.y;
}
void tr_update(int x){
	update(1,1,n,id[x],depth[x],x);
}
int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n-1;i++){
		int u,v;cin>>u>>v;
		G[u].pb(v);G[v].pb(u);
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	tr_update(1);
	while(m--){
		char op;cin>>op;
		if(op=='Q'){
			int x;cin>>x;
			cout<<tr_query(1,x)<<"\n";
		}
		else{
			int x;cin>>x;
			tr_update(x);
		}
	}
}
/*
5 1 
1 2 
1 3 
2 4 
2 5 
Q 2
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

minato_yukina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值