treap启发式合并
注意输入v要在建根的前面。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <ctime>
#include <queue>
using namespace std;
const int maxn = + ;
int tot = , f[maxn], n, Q, v[maxn];
char cmd;
struct Node{
int r, v, s;
Node* ch[];
void maintain(){
s = ch[] -> s + ch[] -> s + ;
return ;
}
}*null = new Node(), *root[maxn], nodes[maxn];
queue<Node*> RAM;
Node* node(){
Node* o;
if(!RAM.empty()) o = RAM.front(), RAM.pop();
else o = &nodes[tot ++];
return o;
}
void del(Node* &o){
RAM.push(o);
o = null;
return ;
}
void init(Node* &o, int v){
o -> ch[] = o -> ch[] = null;
o -> s = ;
o -> r = rand();
o -> v = v;
return ;
}
void rotate(Node* &o, int d){
Node* k = o -> ch[d ^ ]; o -> ch[d ^ ] = k -> ch[d]; k -> ch[d] = o;
o -> maintain(); k -> maintain(); o = k; return ;
}
void insert(Node* &o, int v){
if(o == null) o = node(), init(o, v);
else{
int d = v > o -> v;
insert(o -> ch[d], v);
if(o -> ch[d] -> r > o -> r) rotate(o, d ^ );
else o -> maintain();
}
return ;
}
void remove(Node* &o, int v){
if(v == o -> v){
if(o -> ch[] != null && o -> ch[] != null){
int d = o -> ch[] -> r > o -> ch[] -> r;
rotate(o, d); remove(o -> ch[d], v);
}
else{
Node* k = o;
if(o -> ch[] != null) o = o -> ch[];
else o = o -> ch[];
del(k);
}
}
else remove(o -> ch[v > o -> v], v);
if(o != null) o -> maintain();
return ;
}
void print(Node* &o){
if(o == null) return ;
print(o -> ch[]);
printf("%d ", o -> v);
print(o -> ch[]);
return ;
}
int kth(Node* &o, int k){
if(o -> s < k || k < ) return -;
if(o -> ch[] -> s + == k) return o -> v;
if(o -> ch[] -> s >= k) return kth(o -> ch[], k);
return kth(o -> ch[], k - o -> ch[] -> s - );
}
void merge(Node* &left, Node* &right){
if(left == null) return ;
merge(left -> ch[], right);
merge(left -> ch[], right);
insert(right, left -> v);
del(left); return ;
}
int findset(int x){
return x == f[x] ? x : f[x] = findset(f[x]);
}
void merge(int a, int b){
a = findset(a); b = findset(b);
if(a == b) return ;
if(root[a] -> s > root[b] -> s) swap(a, b);
merge(root[a], root[b]);
f[a] = b; return ;
}
void read(int &x){
x = ; int sig = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') sig = -; ch = getchar(); }
while(isdigit(ch)) x = * x + ch - '', ch = getchar();
x *= sig; return ;
}
void init(){
srand(time());
null -> s = ;
read(n); read(Q);
for(int i = ; i <= n; i ++) read(v[i]);
for(int i = ; i <= n; i ++) f[i] = i, root[i] = node(), init(root[i], v[i]);
return ;
}
void work(){ return ;
}
void print(){ return ;
}
int main(){
init();
work();
print();
return ;
}
treap启发式合并的更多相关文章
- BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...
- 【bzoj2733】[HNOI2012]永无乡 Treap启发式合并
题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
- BZOJ4919 大根堆(动态规划+treap+启发式合并)
一个显然的dp是设f[i][j]为i子树内权值<=j时的答案,则f[i][j]=Σf[son][j],f[i][a[i]]++,f[i][a[i]+1~n]对其取max.这样是可以线段树合并的, ...
- 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】
题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...
- 【bzoj2733】永无乡(无旋treap启发式合并 + 并查集)
传送门 题目分析 起初每个岛都是一个平衡树, 并查集的祖先都是自己.合并两岛时,pri较小的祖先会被作为合并后的祖先, 而两颗平衡树采用启发式合并.查询k值就是基本操作. code #include& ...
- BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- 启发式合并&线段树合并/分裂&treap合并&splay合并
启发式合并 有\(n\)个集合,每次让你合并两个集合,或询问一个集合中是否存在某个元素. 我们可以用平衡树/set维护集合. 对于合并两个\(A,B\),如果\(|A|<|B|\),那么 ...
- BZOJ 2733 [HNOI2012]永无乡(启发式合并+Treap+并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2733 [题目大意] 给出n个点,每个点都有自己的重要度,现在有连边操作和查询操作, 查 ...
- SCUT - 106 - 花式ac - 主席树/启发式合并Treap
https://scut.online/p/106 错在这组样例,发现是离散化之后,对k访问的时候也是应该访问离散化之后的k. 12 4 1 1 2 2 5 5 4 4 3 3 2 1 1 3 3 5 ...
随机推荐
- Haskell 差点儿无痛苦上手指南
趁着自己重装Linux 虚拟机的机会,把安装 haskell 的过程记录一下,顺便帮那些还犹豫徘徊在haskell门外的读者入门. 基本概念: Haskell : 是一门通用函数式语言,差点儿能够进行 ...
- ExtJS学习-----------Ext.String,ExtJS对javascript中的String的扩展
关于ExtJS对javascript中的String的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...
- [转] linux中cat more less head tail 命令
1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具.查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名. 比如: [root@localhost ~]# c ...
- asp.net错误日志写入
当我们一个web项目开发已完成,测试也通过了后,就把他放到网上去,但是,bug是测不完的,特别是在一个大的网络环境下.那么,我们就应该记录这些错误,然后改正.这里,我的出错管理页面是在global.a ...
- JavaScript Unicode字符操作
charCodeAt() 方法 定义和用法charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数.方法 charCodeAt() 与 ...
- 导入excel错误:外部表不是预期的格式 解决方案(Oledb)
-----转载:http://blog.csdn.net/zhou349398998/article/details/8740424 环境:win7+iis7+Office2007 在asp.net网 ...
- Oracle创建表空间以及用户,并授权
//创建临时表空间 create temporary tablespace testtemp tempfile 'D:/app/Administrator/oradata/testdata/te ...
- UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:
2015-01-28 21:55:17.790 Demo[636:9351] the behavior of the UICollectionViewFlowLayout is notdefined ...
- [转]Delphi : keydown与keypress的区别,组合键
Shift 是一个集合变量. type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDoubl ...
- MySQL索引及Explain及常见优化
MySQL索引设计的原则 1. 搜索的索引列,不一定是所要选择的列.换句话说,最适合索引的列是出现在WHERE 子句中的列,或连接子句中指定的列,而不是出现在SELECT 关键字后的选择列表中的列. ...