注意输入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启发式合并的更多相关文章

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

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

  2. 【bzoj2733】[HNOI2012]永无乡 Treap启发式合并

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

  3. 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.这样是可以线段树合并的, ...

  4. 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】

    题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...

  5. 【bzoj2733】永无乡(无旋treap启发式合并 + 并查集)

    传送门 题目分析 起初每个岛都是一个平衡树, 并查集的祖先都是自己.合并两岛时,pri较小的祖先会被作为合并后的祖先, 而两颗平衡树采用启发式合并.查询k值就是基本操作. code #include& ...

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

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

  7. 启发式合并&线段树合并/分裂&treap合并&splay合并

    启发式合并 有\(n\)个集合,每次让你合并两个集合,或询问一个集合中是否存在某个元素. ​ 我们可以用平衡树/set维护集合. ​ 对于合并两个\(A,B\),如果\(|A|<|B|\),那么 ...

  8. BZOJ 2733 [HNOI2012]永无乡(启发式合并+Treap+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2733 [题目大意] 给出n个点,每个点都有自己的重要度,现在有连边操作和查询操作, 查 ...

  9. 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 ...

随机推荐

  1. 百度地图api之如何自定义标注图标

    在百度地图api中,默认的地图图标是一个红色的椭圆形.但是在项目中常常要求我们建立自己的图标,类似于我的这个 操作很简单,分如下几步进行 步骤一:先ps一个图标,大小要合适,如果要背景透明的,记得保存 ...

  2. Analyzing UI Performance with Systrace 使用systrace工具分析ui性能

    While developing your application, you should check that user interactions are buttery smooth, runni ...

  3. HUD2087

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using name ...

  4. jquery获取元素到页面顶部距离

    jquery获取元素到页面顶部距离的语句为: $(selector).offset().top

  5. (转)PHP中构造函数和析构函数解析

    --http://www.jb51.net/article/56047.htm 构造函数 void __construct ([ mixed $args [, $... ]] ) PHP 5 允行开发 ...

  6. Javascript闭包简单理解

    提到闭包,想必大家都早有耳闻,下面说下我的简单理解.平时写代码.第三方框架和组件都或多或少用到了闭包.所以,了解闭包是非常必要的.呵呵... 一.什么是闭包简而言之,就是能够读取其他函数内部变量的函数 ...

  7. 读取url(1

    就书本例子 import java.io.InputStream; import java.net.URL; public class Test { public static void main(S ...

  8. 你好,C++(4)2.1.3 我的父亲母亲:编译器和链接器 2.1.4 C++程序执行背后的故事

    2.1.3  我的父亲母亲:编译器和链接器 从表面上看,我是由Visual Studio创建的,而实际上,真正负责编译源代码创建生成可执行程序HelloWorld.exe的却是Visual Studi ...

  9. Python网页爬虫(一)

    很多时候我们想要获得网站的数据,但是网站并没有提供相应的API调用,这时候应该怎么办呢?还有的时候我们需要模拟人的一些行为,例如点击网页上的按钮等,又有什么好的解决方法吗?这些正是python和网页爬 ...

  10. mysql操作1

    一.连接MYSQL.格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root - ...