BZOJ 2733 [HNOI2012]永无乡 (权值线段树启发式合并+并查集)
题意:
n<=1e5的图里,在线连边、查询某连通块第k大
思路:
练习线段树合并的好题,因为依然记得上一次启发式合并trie的时候内存爆炸的恐怖,所以这次还是用了动态开点、回收
听说启发式合并splay更快QAQ,学会了试试
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e5+;
const int maxm = 6e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int n,m;
int q;
int ls[maxn*],rs[maxn*],dat[maxn*];
int root[maxn];
int a[maxn],id[maxn]; int f[maxn];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
} int tot;
queue<int>pool;
int New(){
if(!pool.empty()){
int x = pool.front();
pool.pop();
return x;
}
++tot;
return tot;
}
void del(int x){
if(!x)return;
pool.push(x);
return;
}
int build(int l, int r, int x){
int mid = (l+r)>>;
int p = New();
dat[p]=;
if(l==r)return p;
if(x<=mid)ls[p]=build(l,mid,x);
else rs[p]=build(mid+,r,x);
return p;
}
int merge(int p, int q){// leave p
if(!p)return q;
if(!q)return p;
ls[p]=merge(ls[p],ls[q]);
rs[p]=merge(rs[p],rs[q]);
dat[p]+=dat[q];
ls[q]=rs[q]=dat[q]=;
del(q);
return p;
}
int query(int x, int l, int r, int k){
int mid = (l+r)>>;
if(l==r)return l;
if(dat[ls[x]]>=k){
return query(ls[x],l,mid,k);
}
else{
return query(rs[x],mid+,r,k-dat[ls[x]]);
}
}
int main() {
scanf("%d %d" ,&n, &m);
for(int i = ; i <= n; i++){
f[i]=i;
scanf("%d", &a[i]);
id[a[i]]=i;
root[i]=build(,n,a[i]);
}
/*for(int i = 1; i <= n; i++){
printf("-- %d root::%d\n",i,root[i]);
}
for(int i = 1; i <= tot; i++){
//printf("%d ==== %d %d %d\n",i,ls[i],rs[i],dat[i]);
}*/
for(int i = ; i <= m; i++){
int x,y;
scanf("%d %d" ,&x, &y);
int t1 = find(x);
int t2 = find(y);
if(t1!=t2){
root[t1]=merge(root[t1],root[t2]);
f[t2]=t1;
}
}
scanf("%d", &q);
while(q--){
char op[];
int x,y;
scanf("%s %d %d", op,&x,&y);
if(op[]=='Q'){
int t = find(x);
if(dat[root[t]]<y){printf("-1\n");continue;}
else printf("%d\n",id[query(root[t],,n,y)]);
}
else{
int t1 = find(x);
int t2 = find(y);
if(t1!=t2){
root[t1]=merge(root[t1],root[t2]);
f[t2]=t1;
}
}
}
return ;
}
/*
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
*/
BZOJ 2733 [HNOI2012]永无乡 (权值线段树启发式合并+并查集)的更多相关文章
- BZOJ2733/LG3324 「HNOI2014」永无乡 权值线段树合并
问题描述 BZOJ2733 LG3224 题解 对于每个结点建立一棵权值线段树. 查询操作就去查询第 \(k\) 大,合并操作就合并两颗权值线段树. 并查集维护连通性. 同时 STO hkk,zcr, ...
- 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]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- 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]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3955 Solved: 2112[Submit][Statu ...
- bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】
bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...
随机推荐
- 解决 Table ‘performance_schema.session_variables’ doesn’t exist 问题
performance_schema在mysql5.5以上就有自带 performance_schema(安装数据库时自带的)如果装数据库或者使用数据时不小心删除了,就会出现Table‘perform ...
- 简单了解linux内核
linux内核是单块结构Linux能动态的按需装载或卸载模块Linux内核线程以一种十分受限制的方式来周期性地执行几个内核函数,因为linux内核线程不能执行用户程序,因此,她们并不代表基本的可执行上 ...
- 《提升能力,涨薪可待》—Java并发之Synchronized
Synchronized简介 线程安全是并发编程中的至关重要的,造成线程安全问题的主要原因: 临界资源, 存在共享数据 多线程共同操作共享数据 而Java关键字synchronized,为多线程场景下 ...
- [论文翻译]Practical Diversified Recommendations on YouTube with Determinantal Point Processes
目录 ABSTRACT(摘要) 1 INTRODUCTION(简介) 2 RELATED WORK 2.1 Diversification to Facilitate Exploration(对应多样 ...
- 【Java编程思想阅读笔记】Java数据存储位置
Java数据存储位置 P46页有感 一.前置知识 栈是由系统自动分配的,Java程序员对栈没有直接的操作权限, 堆是所有线程共享的内存区域,栈 是每个线程独享的. 堆是由程序员自己申请的,在使用new ...
- Redux 一步到位
简介 Redux 是 JavaScript 状态容器,提供可预测化的状态管理 Redux 除了和 React 一起用外,还支持其它库( jquery ... ) 它体小精悍(只有2kB,包括依赖) 由 ...
- 【聚类评价】Calinski-Harabaz(CH)
Calinski-Harabaz(CH) CH指标通过计算类中各点与类中心的距离平方和来度量类内的紧密度,通过计算各类中心点与数据集中心点距离平方和来度量数据集的分离度,CH指标由分离度与紧密度的比值 ...
- kaggle预测房价的代码步骤
# -*- coding: utf-8 -*- """ Created on Sat Oct 20 14:03:05 2018 @author: 12958 " ...
- idea怎么关闭项目
原文地址:https://jingyan.baidu.com/article/a3a3f8112169e78da2eb8a8d.html idea关闭项目可以按File-CloseProject按钮实 ...
- Windows7只能设置纯色背景解决方法
解决设置设置纯色图片,不能设置其他背景图片的方法. 比如这样的. 首先找到这个目录 C:\Users\(这个位置填写你的电脑用户名)\AppData\Roaming\Microsoft\Windows ...