【bzoj】2733: [HNOI2012]永无乡
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
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
2
5
1
2
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 100100
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,Q;
char ch; struct TREAP
{
llg size;
struct
{
llg l,r,rnd,val,size,fa;
}po[maxn]; void update(llg x){po[x].size=po[po[x].l].size+po[po[x].r].size;} llg right_(llg x)
{
llg t=po[x].l;
po[x].l=po[t].r; po[po[t].r].fa=x;
po[t].r=x; po[x].fa=t;
po[t].size=po[x].size;
update(x);
return t;
} llg left_(llg x)
{
llg t=po[x].r;
po[x].r=po[t].l; po[po[t].l].fa=x;
po[t].l=x; po[x].fa=t;
po[t].size=po[x].size;
update(x);
return t;
} void init()
{
size=; memset(po,,sizeof(po));
for (llg i=;i<=n;i++) scanf("%lld",&po[i].val),po[i].fa=i;
} llg insert(llg x,llg wz,llg lx)
{
if (x==)
{
po[wz].size=; po[wz].fa=lx;
return wz;
}
po[x].size++;
if (po[x].val<po[wz].val)
{
po[x].r=insert(po[x].r,wz,x);
if (po[po[x].r].rnd<po[x].rnd) x=left_(x);
}
else
{
po[x].l=insert(po[x].l,wz,x);
if (po[po[x].l].rnd<po[x].rnd) x=right_(x);
}
po[x].fa=lx;
return x;
} llg find_root(llg x)
{
while (po[x].fa!=x)
x=po[x].fa;
return x;
} void dfs(llg x,llg y)
{
if (x==) return ;
dfs(po[x].l,y);
dfs(po[x].r,y);
insert(y,x,y);
} void union_(llg x,llg y)
{
llg f1=find_root(x),f2=find_root(y);
if (f1==f2) return ;
if (po[f1].size<po[f2].size) swap(f1,f2);
dfs(f2,f1);
} llg rank(llg x,llg res)
{
if (res==po[po[x].l].size+) return x;
if (res<=po[po[x].l].size) return rank(po[x].l,res);
else return rank(po[x].r,res-po[po[x].l].size-);
}
}tree; int main()
{
yyj("a");
cin>>n>>m;
tree.init();
for (llg i=;i<=m;i++)
{
llg x,y;
scanf("%lld%lld",&x,&y);
tree.union_(x,y);
}
cin>>Q;
while (Q--)
{
llg x,y;
ch=getchar();
while (ch!='Q' && ch!='B') ch=getchar();
scanf("%lld%lld",&x,&y);
if (ch=='Q')
{
llg ans=tree.rank(tree.find_root(x),y);
if (ans==) ans=-;
printf("%lld\n",ans);
}
else
{
tree.union_(x,y);
}
}
return ;
}
【bzoj】2733: [HNOI2012]永无乡的更多相关文章
- BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- bzoj 2733: [HNOI2012]永无乡 离线+主席树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1167 Solved: 607[Submit][Status ...
- BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- bzoj 2733: [HNOI2012]永无乡 -- 线段树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
- Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3955 Solved: 2112[Submit][Statu ...
- Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...
- 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡
题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...
- bzoj 2733: [HNOI2012]永无乡
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- bzoj 2733 : [HNOI2012]永无乡 (线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- [js]js中原型的继承
js继承01 思路: 单例/工厂/构造函数--演进到原型 搞清原型结构 原型继承 模拟系统原型继承 实现自己的继承 观察原型继承特点 演进到原型链这一步 //单例模式: 防止变量名冲突: // 思路: ...
- error.jsp错误页面跳转,统一异常处理
常见web项目中会用倒计时然后跳转页面来处理异常 error.jsp关键代码: <script language="javascript" type="text/j ...
- minus查找两张表的不同项
minus关键字的使用: select * from A minus select * from B; 上面的SQL语句返回的是表A中存在,表B中不存在的数据: 注意:1.区分不同的规则是查询的所有字 ...
- C#webBrowser使用代理服务器的方法winform
其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧.这个其实是调用一个Dll文件进行设置的, ...
- Pandas之Dropna滤除缺失数据
import pandas as pd import numpy as np from numpy import nan as NaN 一.处理Series对象 通过dropna()滤除缺失数据 fr ...
- eigen quick reference
参考: http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt // A simple quickref for Eigen. Add anyth ...
- selenium webdriver testng自动化测试数据驱动
selenium webdriver testng自动化测试数据驱动 selenium webdriver testng自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...
- Quick Look at the Air Jordan 32
A color with 25 years of history in the Air Jordan line will once again leave its mark on the Air Jo ...
- Twitter OA prepare: Two Operations
准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...
- 登陆跳板机每天只输入一次token的方法——ssh clone session
自从跳板机升级后,无所不在的token让小PE很是恼火,于是有了这篇文章@_@ Linux or Mac篇 在Fedora或者Mac下很简单,修改~/.ssh/config文件,没有的话,就新建一个( ...