3
24
2015
1

2733: [HNOI2012]永无乡

2733: [HNOI2012]永无乡

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1398  Solved: 738
[Submit][Status][Discuss]

Description

永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输出那个岛的编号。 
 

Input

输入文件第一行是用空格隔开的两个正整数 n 和 m,分别 表示岛的个数以及一开始存在的桥数。接下来的一行是用空格隔开的 n 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。 对于 20%的数据 n≤1000,q≤1000 
 
对于 100%的数据 n≤100000,m≤n,q≤300000 
 

Output

对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-1。 
 

Sample Input

5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3

Sample Output

-1
2
5
1
2

并查集维护连通性

权值线段树查询第k大

然后就是线段树合并

不想写splay= =

#include <cstdio>
#include <algorithm>
using namespace std;
int a[100005],b,c,d,e,f,g,h,i,j,k,l,m,n;
int id[100005],ls[2000005],rs[2000005],size[2000005],root[100005];
int fa[100005],tot;
int get(int x){if(fa[x]==x)return x;return fa[x]=get(fa[x]);}
char S[100005];

void ask(int x,int l,int r,int s){
	if(l==r){h=l;return;}
	int mid=(l+r)>>1;
	if(size[ls[x]]>=s)ask(ls[x],l,mid,s);
	else ask(rs[x],mid+1,r,s-size[ls[x]]);
}

int merge(int p,int q){
	if(!(p*q))return p+q;
	ls[p]=merge(ls[p],ls[q]);
	rs[p]=merge(rs[p],rs[q]);
	size[p]=size[ls[p]]+size[rs[p]];
	return p;
}

void insert(int &x,int l,int r,int s){
	if(!x)x=++tot;
	if(l==r){
		size[x]++;return;
	}
	int mid=(l+r)>>1;
	if(s<=mid)insert(ls[x],l,mid,s);
	else insert(rs[x],mid+1,r,s);
	size[x]=size[ls[x]]+size[rs[x]];
}

int main(){
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){scanf("%d",&a[i]);id[a[i]]=i;}
	for(i=1;i<=n;i++)fa[i]=i;
	for(i=1;i<=m;i++){
		scanf("%d%d",&b,&c);
		b=get(b);c=get(c);
		fa[b]=c;
	}
	for(i=1;i<=n;i++){
		insert(root[get(i)],1,n,a[i]);
	}
	scanf("%d",&m);
	for(;m;m--){
		scanf("%s",S);scanf("%d%d",&b,&c);
		if(S[0]=='Q'){
			b=get(b);
			if(size[root[b]]<c){printf("-1\n");continue;}
			ask(root[b],1,n,c);
			printf("%d\n",id[h]);
		}
		else{
			b=get(b);c=get(c);
			fa[b]=c;
			root[c]=merge(root[b],root[c]);
		}
	}
	return 0;
}
Category: BZOJ | Tags: | Read Count: 628
pavzi.com 说:
2024年1月22日 14:18

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us.pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com