题目链接:http://hihocoder.com/problemset/problem/1329

这题本来是学Splay的题,但是我为了练习Treap的Split和Merge操作,就借来用一用。

就是砍树然后再合并。

存个代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
#include <functional>
#include <queue>
#include <stack>
#include <list>
#include <ctime>
using namespace std; const int inf = ~0U >> ; typedef long long LL; struct TreapNode {
int key, val, siz;
TreapNode* ch[];
TreapNode() :val(), siz(){ key = rand(); ch[] = ch[] = NULL; }
void rz(){
siz = ;
if (ch[]) siz += ch[]->siz;
if (ch[]) siz += ch[]->siz;
}
~TreapNode(){
if (ch[]) delete ch[];
if (ch[]) delete ch[];
}
}; typedef pair<TreapNode*, TreapNode*> DTreap; void rot(TreapNode*& rt, bool d){
TreapNode* c = rt->ch[d];
rt->ch[d] = c->ch[!d];
c->ch[!d] = rt;
rt->rz(); c->rz();
rt = c;
} void Insert(TreapNode*& rt, int val, int key = ) {
if (!rt){
rt = new TreapNode();
rt->val = val;
if (key) rt->key = key;
return;
}
//if (val == rt->val) return;
bool d = val > rt->val;
Insert(rt->ch[d], val, key);
if (rt->ch[d]->key < rt->key) rot(rt, d);
else rt->rz();
} void Delete(TreapNode*& rt, int val) {
if (rt == NULL) return;
if (rt->val == val) {
if (rt->ch[] == NULL && rt->ch[] == NULL) {
delete rt;
rt = NULL;
return;
}
else if (rt->ch[] == NULL) {
rot(rt, );
Delete(rt->ch[], val);
}
else if (rt->ch[] == NULL) {
rot(rt, );
Delete(rt->ch[], val);
}
else {
bool d = rt->ch[]->key > rt->ch[]->key;
rot(rt, d);
Delete(rt->ch[!d], val);
}
}
else {
bool d = val > rt->val;
Delete(rt->ch[d], val);
}
rt->rz();
} int Kth(TreapNode* rt, int k) {
if (!rt) return -inf;
if (rt->siz < k) return -inf;
int r = ;
if (!rt->ch[]) r = ;
else r = rt->ch[]->siz;
if (r == k - ) return rt->val;
if (k - < r) return Kth(rt->ch[], k);
return Kth(rt->ch[], k - r - );
} DTreap Split(TreapNode* rt, int val) {
DTreap ret;
Insert(rt, val, -inf);
ret.first = rt->ch[];
ret.second = rt->ch[];
rt->ch[] = rt->ch[] = NULL;
delete rt;
return ret;
} void Merge(DTreap dt, TreapNode*& rt) {
rt = NULL;
TreapNode* tch = dt.first?dt.first:dt.second;
if(tch) {
TreapNode* nd = new TreapNode();
nd->key = -inf;
nd->val = tch->val;
nd->ch[] = dt.first;
nd->ch[] = dt.second;
rt = nd;
Delete(rt, nd->val);
}
} int ans; void Query(TreapNode* rt, int x) {
if (rt == NULL) return;
if (rt->val == x) {
ans = x;
return;
}
if (rt->val > x) {
Query(rt->ch[], x);
}
else {
ans = max(ans, rt->val);
Query(rt->ch[], x);
}
} char cmd[];
int a, b, n; int main() {
srand(time(NULL));
scanf("%d", &n);
TreapNode* root = NULL;
for (int i = ; i < n; i++) {
scanf("%s", cmd);
if (cmd[] == 'I') {
scanf("%d", &a);
Insert(root, a);
}
else if (cmd[] == 'Q') {
ans = -inf;
scanf("%d", &a);
Query(root, a);
printf("%d\n", ans);
}
else {
scanf("%d%d", &a, &b);
DTreap dt = Split(root, a);
TreapNode* lch = dt.first;
TreapNode* rch = dt.second;
dt = Split(rch, b + );
if (dt.first) delete dt.first;
dt.first = lch;
Merge(dt, root);
}
}
if (root) delete root;
return ;
}

[hihoCoder1329] 带Split和Merge的Treap的更多相关文章

  1. opencv —— split、merge 通道的分离与合并

    对于三通道或四通道图像,有时要对某一通道的像素值进行修改或展示,这就需要进行通道分离操作.修改后,若要进行结果展示,就需要重新将各通道合并. 通道分离:split 函数 void split (Inp ...

  2. split()函数+merge()函数

    在图像处理时,我们接触到的彩色以RGB居多,为了分析图像在某一通道上的特性,需要将图像的颜色通道进行分离,或者是在对某一颜色通道处理后重新进行融合.opencv提供了split()函数来进行颜色通道的 ...

  3. Partitioning & Archiving tables in SQL Server (Part 2: Split, Merge and Switch partitions)

    Reference: http://blogs.msdn.com/b/felixmar/archive/2011/08/29/partitioning-amp-archiving-tables-in- ...

  4. 【数据结构】【平衡树】无旋转treap

    最近在研究平衡树,看起来这种东西又丧水又很深,感觉很难搞清楚.在Ditoly学长的建议下,我先学习了正常的treap,个人感觉这应该是平衡树当中比较好懂的而且比较好写的一种. 然而,发现带旋treap ...

  5. 飞旋treap

    虽然叫做非旋treap但是飞旋treap很带感所以就用这个名字了(SB) 这个东西是真的好写...... 主要的两个函数只有两个,rotate和splay,split和merge. merge就是大家 ...

  6. 非旋转Treap

    Treap是一种平衡二叉树,同时也是一个堆.它既具有二叉查找树的性质,也具有堆的性质.在对数据的查找.插入.删除.求第k大等操作上具有期望O(log2n)的复杂度.     Treap可以通过节点的旋 ...

  7. 平衡树合集(Treap,Splay,替罪羊,FHQ Treap)

    今天翻了翻其他大佬的博客,发现自己有些...颓废... 有必要洗心革面,好好学习 序:正常的BST有可能退化,成为链,大大降低效率,所以有很多方法来保持左右size的平衡,本文将简单介绍Treap,S ...

  8. 洛谷-P3369-普通平衡树(Treap)

    题目传送门 标题说平衡树,那么应该AVL,红黑树都能过,但是这次做这题主要是学习Treap,所以花了几天搞出了这题.其他方法以后再说吧 Treap(带旋转) #include <bits/std ...

  9. [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

    转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec  Mem ...

随机推荐

  1. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

  2. python 读取并显示图片的两种方法

    在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片.本人偏爱 matpoltlib,因为它的语法更像 matlab. 一.matplotlib 1. ...

  3. MSP430单片机的两种SPI总线实现方式

    MSP430单片机上的SPI总线的实现方式分为两种:硬件实现和软件实现. 二者的抽象层次不同,硬件实现方式下程序员只需要完成总线协议的寄存器层,即一字节(char,8位二进制)数据,而软件实现方式下程 ...

  4. UE4 VR 模式全屏 4.13

    以前写了一个4.11版本全屏,高版本的没用所以也不清楚情况,最近出了4.13,刚好新项目要用上打包出来以后发现,控制台命令fullscreen没有用了, 被stereo on 替代,但是还是没有全屏, ...

  5. 转:StrictMode使用

    最新的Android平台中(Android 2.3起),新增加了一个新的类,叫StrictMode(android.os.StrictMode).这个类可以用来帮助开发者改进他们编写的应用,并且提供了 ...

  6. Quartus II 与 Modelsim 联调【转】

    Quartus II 9.0版本的时候软件还有自带的仿真工具,现在安装的是11.0以上版本,才发现 Quartus II 11.0以上取消了软件自带的波形仿真工具,因此需要波形仿真就要调用专业的仿真工 ...

  7. circular_buffer

    编程有时需要使用定长的容器(fixed size container).实现旋转容器可以像下面这样: std::vector<T> vec(size); vec[i % size] = n ...

  8. c++句柄设计

    句柄,也称为智能指针. 我计算了一下我的时间,以后每14天得读完一本书,才不愧对我买的这么多书.然而我还要抽出时间来谢谢博文.最近读的是c++沉思录,开篇就用了3章来讲述句柄.好了,废话少说,接下来谈 ...

  9. 印刷电路板(PCB)的材料

    以玻璃为基础材料的板材可以在高达150℃到250℃的温度下使用.可选的介质材料有: FR4,介电常数ε0为4.6 环氧材料,介电常数ε0为3.9: 聚酰亚胺,介电常数ε0为4.5. 另外,以聚四氟乙烯 ...

  10. git初学习体会

    github:项目版本控制器 git和传统的版本控制器相比,最大的一点是,界面简单,给与非线性开发模式的强有力的支持,完全分布式等. 对于完全分布式的实现,我的理解是这个样子的.这多少要涉及到一点它的 ...