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

平衡树启发式合并

每次合并都把小的拆开,放到大的里面(听说数据很水,随机的,不旋转就可以过,写了一个,然后就pascal的rank1了)

还是正常一点写一个splay吧(不过常数有点大,所以是深度超过32才splay)

 const
maxn=;
dep=;
type
node=record
son:array[..]of longint;
size,data,fa:longint;
end;
var
tree:array[..maxn]of node;
f,root:array[..maxn]of longint;
n,m,q:longint; function find(x:longint):longint;
begin
if f[x]=x then exit(x);
f[x]:=find(f[x]);
exit(f[x]);
end; procedure rotate(x,w:longint;var root:longint);
var
y:longint;
begin
y:=tree[x].fa;
tree[y].son[w]:=tree[x].son[w xor ];
if tree[x].son[w xor ]<> then tree[tree[x].son[w xor ]].fa:=y;
tree[x].son[w xor ]:=y;
if y=root then root:=x
else
if tree[tree[y].fa].son[]=y then tree[tree[y].fa].son[]:=x
else tree[tree[y].fa].son[]:=x;
tree[x].fa:=tree[y].fa;
tree[y].fa:=x;
tree[y].size:=tree[tree[y].son[]].size+tree[tree[y].son[]].size+;
end; procedure splay(x:longint;var root:longint);
var
y:longint;
begin
while root<>x do
begin
y:=tree[x].fa;
if y=root then
if tree[y].son[]=x then rotate(x,,root)
else rotate(x,,root)
else
if tree[tree[y].fa].son[]=y then
if tree[y].son[]=x then
begin
rotate(y,,root);
rotate(x,,root);
end
else
begin
rotate(x,,root);
rotate(x,,root);
end
else
if tree[y].son[]=x then
begin
rotate(x,,root);
rotate(x,,root);
end
else
begin
rotate(y,,root);
rotate(x,,root);
end;
end;
tree[x].size:=tree[tree[x].son[]].size+tree[tree[x].son[]].size+;
end; procedure insert(x:longint;var root:longint);
var
now,step:longint;
begin
now:=root;
step:=;
while true do
begin
inc(step);
inc(tree[now].size);
if tree[x].data<tree[now].data then
if tree[now].son[]= then break
else now:=tree[now].son[]
else
if tree[now].son[]= then break
else now:=tree[now].son[];
end;
if tree[x].data<tree[now].data then tree[now].son[]:=x
else tree[now].son[]:=x;
tree[x].size:=;
tree[x].son[]:=;
tree[x].son[]:=;
tree[x].fa:=now;
if step>dep then splay(x,root);
end; procedure dfs(x:longint;var root:longint);
begin
with tree[x] do
begin
if son[]<> then dfs(son[],root);
if son[]<> then dfs(son[],root);
insert(x,root);
end;
end; procedure union(x,y:longint);
var
u,v:longint;
begin
u:=find(x);
v:=find(y);
if u=v then exit;
if tree[root[u]].size<tree[root[v]].size then
begin
dfs(root[u],root[v]);
f[u]:=v;
end
else
begin
dfs(root[v],root[u]);
f[v]:=u;
end;
end; procedure init;
var
i,x,y:longint;
begin
read(n,m);
for i:= to n do
begin
f[i]:=i;
root[i]:=i;
tree[i].size:=;
end;
for i:= to n do
read(tree[i].data);
for i:= to m do
begin
read(x,y);
union(x,y);
end;
end; function ans(k:longint;var root:longint):longint;
var
now,step:longint;
begin
now:=root;
step:=;
if k>tree[now].size then exit(-);
while true do
begin
inc(step);
if k=tree[tree[now].son[]].size+ then
begin
if step>dep then splay(now,root);
exit(now);
end;
if k<=tree[tree[now].son[]].size then now:=tree[now].son[]
else
begin
dec(k,tree[tree[now].son[]].size+);
now:=tree[now].son[];
end;
end;
end; procedure work;
var
i,x,y:longint;
c:char;
begin
readln(q);
for i:= to q do
begin
readln(c,x,y);
if c='Q' then
begin
if (x>n) or (x<) then writeln(-)
else writeln(ans(y,root[find(x)]));
end
else union(x,y);
end;
end; begin
init;
work;
end.

2733: [HNOI2012]永无乡 - BZOJ的更多相关文章

  1. BZOJ 2733: [HNOI2012]永无乡 启发式合并treap

    2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. bzoj 2733: [HNOI2012]永无乡 离线+主席树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1167  Solved: 607[Submit][Status ...

  3. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)

    不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...

  4. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  5. bzoj 2733: [HNOI2012]永无乡 -- 线段树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...

  6. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  7. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  8. 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡

    题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...

  9. bzoj 2733: [HNOI2012]永无乡

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

随机推荐

  1. HTTP - 摘要认证

    基本认证便捷灵活,但极不安全.用户名和密码都是以明文形式传送的,也没有采取任何措施防止对报文的篡改.安全使用基本认证的唯一方式就是将其与 SSL 配合使用. 摘要认证是另一种 HTTP 认证协议,它与 ...

  2. 【MINA】缓存区ByteBuffer和IOBuffer你要了解的常用知识

    mina中IOBuffer是Nio中ByteBuffer的衍生类,主要是解决Bytebuffer的两个不足 1.没有提供足够灵活的get/putXXX方法 2.它容量固定,难以写入可变长度的数据 特点 ...

  3. 第四节 使用XML

    可扩展标记语言XML,是一种平台无关的数据表示格式,对特定数据表示需求的数据来说,比关系型格式有一定的优势. XML文档可以是计算机上的物理文件,网络上的数据流,或者仅仅是内存中的字符串.但是,XML ...

  4. response小结(二)——文件下载

    我们先来看一个最简单的文件下载的例子: package com.yyz.response; import java.io.FileInputStream; import java.io.IOExcep ...

  5. Access和Sql区别

    假设表game有一字段为gameYuiJian为bit字段(SQL SERVER 20005)和"是/否"字段(ACCSS数据库),在编写脚本文件时,如下才能正确执行 SQL st ...

  6. centos6.5下磁盘分区及挂载

    1..查看磁盘空间 2.磁盘分区 3.格式化分区 4.挂载/卸载

  7. linux umount 提示device is busy 的解决

    linux umount 提示"device is busy" 终极解决 为了干净地关闭或热交换 UNIX 或类 UNIX 系统上的存储硬件,必须能够卸载使用此设备上的存储的所有文件系统.但是,如果正 ...

  8. Build Firefox 编译Firefox

    准备 选择需要的firefox版本 http://hg.mozilla.org/releases/ 选择最新的build工具 http://ftp.mozilla.org/pub/mozilla.or ...

  9. JavaScript代码检查工具 — JSHint

    静态代码检查是开发工作中不可缺少的一环,毕竟对于程序化的工作人的眼睛是不可靠的,更何况是自己的眼睛看自己的代码.即使最后的运行结果通过,但可能存在一些未定义的变量.定义了但最后没用过的变量.分号有没有 ...

  10. Mysql配置文件my.cnf解析

    # vim /etc/my.cnf [client] port = 3306 //客户端所连接的端口号 socket = /tmp/mysql.sock //客户端所连接的sock文件存放位置 [my ...