题目大意:给定一个无向图,每个点和每条边都有权值,多次询问从点v开始只能经过边权小于等于x的点中权值第k大
此题不强制在线,直接把边和询问都按照边权从小到大排序,初始每个节点是一个Treap的根节点
对于每个询问把小于等于这个询问的权值的边两侧的Treap进行启发式合并 然后求第k大即可
不知道是谁出了个强制在线版……回头研究一下
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 100100
using namespace std;
struct abcd{
abcd *ls,*rs;
int num,key;
int cnt,size;
abcd (int x,int y);
void Maintain();
}*null=new abcd(-19980402,0),*tree[M];
struct query{
int v,x,k,pos;
bool operator < (const query &y) const
{
return x < y.x;
}
}queries[M*5];
struct edge{
int x,y,z;
bool operator < (const edge &Y) const
{
return z < Y.z;
}
}edges[M*5];
int n,m,q;
int a[M],ans[M*5];
int fa[M],size[M];
abcd :: abcd(int x,int y)
{
ls=rs=null;
num=x;
key=x==-19980402?0:rand();
cnt=size=y;
}
void abcd :: Maintain()
{
size=ls->size+rs->size+cnt;
}
void Zig(abcd *&x)
{
abcd *y=x->ls;
x->ls=y->rs;
y->rs=x;
x=y;
x->rs->Maintain();
}
void Zag(abcd *&x)
{
abcd *y=x->rs;
x->rs=y->ls;
y->ls=x;
x=y;
x->ls->Maintain();
}
void Insert(abcd *&x,int y,int z)
{
if(x==null)
{
x=new abcd(y,z);
return ;
}
if(y==x->num)
x->cnt+=z;
else if(y<x->num)
{
Insert(x->ls,y,z);
if(x->ls->key>x->key)
Zig(x);
}
else
{
Insert(x->rs,y,z);
if(x->rs->key>x->key)
Zag(x);
}
x->Maintain();
}
void Decomposition(abcd *&x,int y)
{
if(x==null)
return ;
Decomposition(x->ls,y);
Decomposition(x->rs,y);
Insert(tree[y],x->num,x->cnt);
delete x;
x=null;
}
int Get_Kth(abcd *x,int k)
{
int temp=x->rs->size;
if(k<=temp)
return Get_Kth(x->rs,k);
k-=temp;
if(k<=x->cnt)
return x->num;
k-=x->cnt;
return Get_Kth(x->ls,k);
}
int Find(int x)
{
if(!fa[x])
fa[x]=x,size[x]=1;
if(fa[x]==x)
return x;
return fa[x]=Find(fa[x]);
}
void Merge(int x,int y)
{
x=Find(x);y=Find(y);
if(x==y)
return ;
if(size[x]>size[y])
swap(x,y);
Decomposition(tree[x],y);
size[y]+=size[x];
fa[x]=y;
}
int main()
{
srand(19980402);
int i,j,x,y,z;
cin>>n>>m>>q;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
tree[i]=new abcd(a[i],1);
}
for(i=1;i<=m;i++)
scanf("%d%d%d",&edges[i].x,&edges[i].y,&edges[i].z);
sort(edges+1,edges+m+1);
for(i=1;i<=q;i++)
{
scanf("%d%d%d",&queries[i].v,&queries[i].x,&queries[i].k);
queries[i].pos=i;
}
sort(queries+1,queries+q+1);
for(i=1,j=1;i<=q;i++)
{
for(;j<=m&&edges[j].z<=queries[i].x;j++)
Merge(edges[j].x,edges[j].y);
queries[i].v=Find(queries[i].v);
if(size[queries[i].v]<queries[i].k)
ans[queries[i].pos]=-1;
else
ans[queries[i].pos]=Get_Kth(tree[queries[i].v],queries[i].k);
}
for(i=1;i<=q;i++)
printf("%d\n",ans[i]);
}