#include
<
iostream
>
using
namespace
std;
int
n, m;
#define
N 200010
#define
lowbit(x) ( (x)&(-(x)) )
struct
TArray{
int
cnt[N];
TArray(){ init(); }
void
update(
int
p,
int
v ){
for
(
int
x
=
p; x
<=
n; cnt[x]
+=
v, x
+=
lowbit(x) ); }
int
sum(
int
p ){
int
x
=
p, s
=
0
;
for
( ; x; s
+=
cnt[x], x
-=
lowbit(x) );
return
s; }
int
rank(
int
k ){
k
=
sum(n)
+
1
-
k;
int
left
=
1
, right
=
n;
while
( left
+
1
<
right ){
int
m
=
(left
+
right)
>>
1
;
int
s
=
sum(m);
if
( s
>=
k ) right
=
m;
else
left
=
m;
}
if
( sum(left)
>=
k )
return
left;
return
right;
}
void
init(){
for
(
int
i
=
0
; i
<=
n;
++
i ) cnt[i]
=
0
; }
};
TArray tay;
struct
U_find{
int
find[N], num[N];
U_find(){ clear();}
int
parent(
int
t ){
int
u
=
t, v;
while
( u
!=
find[u] ) u
=
find[u];
while
( t
!=
u ) { v
=
find[t]; find[t]
=
u; t
=
find[v]; }
return
u; }
bool
is_friend(
int
u,
int
v ){
return
parent(u)
==
parent(v); }
void
set_friend(
int
u,
int
v ){
int
a
=
parent(u), b
=
parent(v);
if
( a
==
b )
return
;
if
( num[a]
>
num[b] ) {
find[b]
=
a;
tay.update( num[b],
-
1
);
tay.update( num[a],
-
1
);
num[a]
+=
num[b];
tay.update( num[a],
1
);
}
else
{
find[a]
=
b;
tay.update( num[a],
-
1
);
tay.update( num[b],
-
1
);
num[b]
+=
num[a];
tay.update( num[b],
1
);
}
}
void
clear(){
for
(
int
i
=
0
; i
<
N;
++
i ) find[i]
=
i, num[i]
=
1
; }
};
U_find uf;
int
main(){
scanf(
"
%d%d
"
,
&
n,
&
m );
tay.update(
1
, n );
while
( m
--
){
int
t, u, v, k;
scanf(
"
%d
"
,
&
t );
if
( t
==
0
){
scanf(
"
%d%d
"
,
&
u,
&
v );
uf.set_friend( u, v );
}
else
{
scanf(
"
%d
"
,
&
k );
printf(
"
%d\n
"
, tay.rank(k) );
}
}
return
0
;
}
posted on 2009-07-14 13:31
Darren 阅读(298)
评论(0) 编辑 收藏 引用 所属分类:
数据结构