BZOJ 3545 [ONTAK2010] Peaks

24 篇文章 0 订阅
9 篇文章 0 订阅

Description

在Bytemountains有N座山峰,每座山峰有他的高度h_i。有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经过困难值小于等于x的路径所能到达的山峰中第k高的山峰,如果无解输出-1。

Input

第一行三个数N,M,Q。
第二行N个数,第i个数为h_i
接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径。
接下来Q行,每行三个数v x k,表示一组询问。

Output

对于每组询问,输出一个整数表示答案。

Sample Input

10 11 4
1 2 3 4 5 6 7 8 9 10
1 4 4
2 5 3
9 8 2
7 8 10
7 1 4
6 7 1
6 4 8
2 1 5
10 8 10
3 4 7
3 4 6
1 5 2
1 5 6
1 5 8
8 9 2

Sample Output

6
1
-1
8


HINT

【数据范围】

N<=10^5, M,Q<=5*10^5,h_i,c,x<=10^9。

Source

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

splay+启发式合并~

离线处理,把询问排序,路径也排序,并查集记录根节点位置,每次按照路径把对应的splay tree合并,求第k大。

注意:1.这里求的是第k小,要反着求。

2.样例里面山的序号和值相同,我觉得是故意的。


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,q,a[100001],c[5000001][2],f[100001],fa[5000001],siz[5000001],root[1000001],ans[500001];

struct node{
	int x,y,val;
}ro[500001];

struct node1{
	int x,val,k,id;
}que[500001];

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

bool operator < (node u,node v)
{
	return u.val<v.val;
}

bool operator < (node1 u,node1 v)
{
	return u.val<v.val;
}

int findd(int u)
{
	return f[u]==u ? u:f[u]=findd(f[u]);
}

void update(int u)
{
	if(!u) return;
	siz[u]=siz[c[u][0]]+siz[c[u][1]]+1;
}

void rotate(int x,int &k)
{
	int y=fa[x],z=fa[y],l,r;
	if(c[y][0]==x) l=0;
	else l=1;r=l^1;
	if(k==y) k=x;
	else if(c[z][0]==y) c[z][0]=x;
	else c[z][1]=x;
	fa[x]=z;fa[y]=x;c[y][l]=c[x][r];
	fa[c[x][r]]=y;c[x][r]=y;
	update(y);update(x);
}

void splay(int x,int &k)
{
	while(x!=k)
	{
		int y=fa[x],z=fa[y];
		if(y!=k)
		{
			if(c[y][0]==x ^ c[z][0]==y) rotate(x,k);
			else rotate(y,k);
		}
		rotate(x,k);
	}
}

void add(int num,int u,int v)
{
	if(!root[v])
	{
		root[v]=num;fa[num]=c[num][0]=c[num][1]=0;siz[num]=1;return;
	}
	int now=root[v],faa;
	while(1)
	{
		faa=now;now=c[now][u>a[now]];
		if(!now)
		{
			siz[num]=1;c[num][0]=c[num][1]=0;
			c[faa][u>a[faa]]=num;fa[num]=faa;
			update(num);update(faa);splay(num,root[v]);return;
		}
	}
}

void del(int u,int v)
{
	if(c[u][0]) del(c[u][0],v);
	if(c[u][1]) del(c[u][1],v);
	add(u,a[u],v);
}

int cal(int k,int v)
{
	while(1)
	{
		if(siz[c[k][0]]>=v) k=c[k][0];
		else if(siz[c[k][0]]+1<v) v-=siz[c[k][0]]+1,k=c[k][1];
		else return k;
	}
}

int main()
{
	n=read();m=read();q=read();
	for(int i=1;i<=n;i++) a[i]=read(),f[i]=i,siz[i]=1,root[i]=i;
	for(int i=1;i<=m;i++) ro[i].x=read(),ro[i].y=read(),ro[i].val=read();
	for(int i=1;i<=q;i++) que[i].x=read(),que[i].val=read(),que[i].k=read(),que[i].id=i;
	sort(ro+1,ro+m+1);sort(que+1,que+q+1);
	for(int k=1,now=1;k<=q;k++)
	{
		if(que[k].val>que[k-1].val)
		  for(;ro[now].val<=que[k].val && now<=m;now++)
		  {
			int x=findd(ro[now].x),y=findd(ro[now].y);
			if(x==y) continue;
			if(siz[root[x]]>siz[root[y]]) swap(x,y);
			del(root[x],y);f[x]=y;
		  }
		int x=findd(que[k].x);
		if(siz[root[x]]<que[k].k) ans[que[k].id]=-1;
		else ans[que[k].id]=a[cal(root[x],siz[root[x]]-que[k].k+1)];
	}
	for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值