A - Count on a tree-----------------------------树上主席树

You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.

We will ask you to perform the following operation:

u v k : ask for the kth minimum weight on the path from node u to node v
Input
In the first line there are two integers N and M. (N, M <= 100000)

In the second line there are N integers. The ith integer denotes the weight of the ith node.

In the next N-1 lines, each line contains two integers u v, which describes an edge (u, v).

In the next M lines, each line contains three integers u v k, which means an operation asking for the kth minimum weight on the path from node u to node v.

Output
For each operation, print its result.

Example
Input:

8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2
Output:
2
8
9
105
7

解析:
树上主席树模板
因为每个节点的主席树是以父亲节点为根建立的。
所以我们一边dfs一遍建主席树.子节点为新版本,父亲节点为旧版本。
u->v一条链上的信息由树上差分可知: sum[u]+sum[v]-sum[lca(u,v)]-sum[fa[lca(u,v)]]。
所以还需要写一个LCA

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int h[N],e[N*3],ne[N*3],idx,tot;
int a[N],b[N];
int root[N];
int len,n,m;
int depth[N],fa[N][20];
vector<int> v;
struct node
{
	int l,r;
	int cnt;
}tr[N*20];
int find(int x)
{
	return lower_bound(b+1,b+len+1,x)-b;
}
void init()
{
	memset(h,-1,sizeof h);
	idx=tot=0;
}
void add(int a,int b)
{
	e[idx]=b;ne[idx]=h[a];h[a]=idx++;
}
void bfs(int u)
{
	memset(depth,0x3f,sizeof depth);
	depth[0]=0;
	depth[u]=1;
	queue<int> q;
	q.push(u);
	while(q.size())
	{
		int t=q.front();
		q.pop();
		for(int i=h[t];~i;i=ne[i])
		{
			int j=e[i];
			if(depth[j]>depth[t]+1)
			{
				depth[j]=depth[t]+1;
				fa[j][0]=t;
				q.push(j);
				for(int k=1;k<=18;k++) fa[j][k]=fa[fa[j][k-1]][k-1];
			}
		
		}
	}
}
int  build(int l,int r)
{
	int p=++tot;
	if(l==r) return p;
	int mid=l+r>>1;
	tr[p].l=build(l,mid);tr[p].r=build(mid+1,r);
	return p;
}
int insert(int p,int l,int r,int x)
{
	int q=++tot;
	tr[q]=tr[p];
	if(l==r)
	{
		tr[q].cnt++;
		return q;
	}
	int mid=l+r>>1;
	if(x<=mid) tr[q].l=insert(tr[p].l,l,mid,x);
	else tr[q].r=insert(tr[p].r,mid+1,r,x);
	tr[q].cnt=tr[tr[q].l].cnt+tr[tr[q].r].cnt;
	return q; 
}
void dfs_build(int q,int p)
{
	root[q]=insert(root[p],1,len,find(a[q]));
	for(int i=h[q];~i;i=ne[i])
	{
		int j=e[i];
		if(j==p) continue;
		dfs_build(j,q);
	 } 
}
int LCA(int x,int y)
{
	if(depth[x]<depth[y]) swap(x,y);
	for(int k=18;k>=0;k--) 
	{
		if(depth[fa[x][k]]>=depth[y]) x=fa[x][k]; 
	}
	if(x==y) return x;
	for(int k=18;k>=0;k--)
	{
		if(fa[x][k]!=fa[y][k])
		{
			x=fa[x][k];
			y=fa[y][k];
		}
	}
	return fa[x][0];
}
int query(int u,int v,int l,int r,int k,int fa,int fsa)
{
	if(l==r) return r;
	int cnt=tr[tr[u].l].cnt+tr[tr[v].l].cnt-tr[tr[fa].l].cnt-tr[tr[fsa].l].cnt;
	int mid=l+r>>1;
	if(k<=cnt) return query(tr[u].l,tr[v].l,l,mid,k,tr[fa].l,tr[fsa].l);
	else return query(tr[u].r,tr[v].r,mid+1,r,k-cnt,tr[fa].r,tr[fsa].r);
}
int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		init();
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			b[i]=a[i];
		}
		sort(b+1,b+1+n);
		 len=unique(b+1,b+1+n)-b-1;
		for(int i=1;i<n;i++)
		{
			int a,b;
			scanf("%d %d",&a,&b);
			add(a,b);add(b,a);
		 } 
		 bfs(1);
		 root[0]=build(1,len);
		 dfs_build(1,0);
		 for(int i=1;i<=m;i++)
		 {
		 	int l,r,op;
		 	scanf("%d %d %d",&l,&r,&op);
		 	int lca=LCA(l,r);
		 	printf("%d\n",b[query(root[l],root[r],1,len,op,root[lca],root[fa[lca][0]])]);
		 }
	}
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值