bzoj 2733 永无乡 线段树
题目:
支持两种操作:
- 合并两点所在的联通块
- 查询某点所在联通块内权值第k小.
题解
平衡树启发式合并随便搞一搞就好了。
我写了一个线段树合并
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 100010;
struct Node{
Node *ch[2];
int siz,id;
void update(){
siz = ch[0]->siz + ch[1]->siz;
}
}*null,mem[maxn*30],*root[maxn],*it;
inline void init(){
it = mem;null = it++;
null->ch[0] = null->ch[1] = null;
null->siz = 0;
}
inline Node* newNode(){
Node *p = it++;p->ch[0] = p->ch[1] = null;
p->siz = 0;return p;
}
inline void insert(Node* &p,int l,int r,int pos,int id){
if(p == null) p = newNode();
if(l == r){
p->siz ++ ;
p->id = id;
return ;
}
int mid = l+r >> 1;
if(pos <= mid) insert(p->ch[0],l,mid,pos,id);
else insert(p->ch[1],mid+1,r,pos,id);
p->update();return ;
}
inline Node* Union(Node *x,Node *y){
if(x == null) return y;
if(y == null) return x;
x->ch[0] = Union(x->ch[0],y->ch[0]);
x->ch[1] = Union(x->ch[1],y->ch[1]);
x->update();return x;
}
int n;
inline int query(Node *p,int k){
if(k < 1 || k > p->siz) return -1;
int l = 1,r = n;
while(1){
if(l == r) return p->id;
int mid = l+r >> 1;
if(p->ch[0]->siz >= k){
p = p->ch[0];
r = mid;
}else{
k -= p->ch[0]->siz;
p = p->ch[1];
l = mid+1;
}
}
}
int fa[maxn];
inline int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main(){
init();int m;read(n);read(m);
for(int i=1;i<=n;++i) root[i] = null,fa[i] = i;
for(int i=1,x;i<=n;++i){
read(x);
insert(root[i],1,n,x,i);
}
for(int i=1,u,v;i<=m;++i){
read(u);read(v);
int x = find(u);
int y = find(v);
if(x == y) continue;
fa[x] = y;
root[y] = Union(root[x],root[y]);
}
int q;read(q);
char ch;
int x,k,u,v;
while(q--){
while(ch=getchar(),ch<'!');
if(ch == 'Q'){
read(x);read(k);
int fx = find(x);
printf("%d\n",query(root[fx],k));
}else if(ch == 'B'){
read(u);read(v);
int x = find(u);
int y = find(v);
if(x == y) continue;
fa[x] = y;
root[y] = Union(root[x],root[y]);
}
}
return 0;
}
bzoj 2733 永无乡 线段树的更多相关文章
- bzoj 2733 永无乡 - 并查集 - 线段树
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...
- bzoj 2733: [HNOI2012]永无乡 -- 线段树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
- Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...
- bzoj 2733 : [HNOI2012]永无乡 (线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- bzoj2733: [HNOI2012]永无乡 线段树合并
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...
- BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并
题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
- [HNOI2012]永无乡 线段树合并
[HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...
- 洛谷P3224 [HNOI2012]永无乡(线段树合并+并查集)
题目描述 永无乡包含 nnn 座岛,编号从 111 到 nnn ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 nnn 座岛排名,名次用 111 到 nnn 来表示.某些岛之间由巨大的桥连接, ...
- 【bzoj2733】[HNOI2012]永无乡 线段树合并
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- HDU 2588 GCD && GCD问题总结
GCD(一) 题目: The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written ( ...
- swagger api 文档框架
<其他教程:https://www.cnblogs.com/FlyAway2013/p/7510279.html> 先看看swagger的生态使用图: 其中,红颜色的是swaggger官网 ...
- Nginx教程
Nginx教程 1.背景 介绍 Nginx是一个高性能的HTTP服务器,以及反向代理服务器 组成 Ngnix有内核和模块组成.微结构的内核根据配置文件将一个请求映射到一个location块中,该loc ...
- LookAndFeelInfo
在读zap源码的时候碰到:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());以下来自百度知道UIManager.s ...
- MyBatis -- 一步步教你使用MyBatis
1.建立开发环境 1.1 创建项目,java项目或者javaweb项目均可,如图: 1.2 加入所须要的jar包到项目lib文件夹下 一个MyBatis-3.2.4.jar包 一个驱动包mysql ...
- You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
[root@localhost dreamstart]# composer installLoading composer repositories with package informationI ...
- MD_STOCK_REQUIREMENTS_LIST_API 取MD04的MRP Element
[转http://lz357502668.blog.163.com/blog/static/16496743201231941718527/]?MD_STOCK_REQUIREMENTS_LIST_A ...
- rtmp播放器
rtmp测试地址: rtmp://live.hkstv.hk.lxdns.com/live 有的时候连接不上,不是很流畅 参考: 1,simplest flashmedia example http: ...
- Python中PIL及Opencv转化
转载:http://blog.sina.com.cn/s/blog_80ce3a550102w26x.html Convert between Python tuple and list a = (1 ...
- Java中byte转换int时与0xff进行与运算的原因
http://w.baike.com/LGAdcWgJBBQxRAHUf.html 转帖 java中byte转换int时为何与0xff进行与运算 在剖析该问题前请看如下代码 public static ...