题目链接: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. sqlite3无法找到

    在DDMS窗口的File Explorer面板下展开system > xbin 看到sqlite3 ,点击右上角的软盘图标(pull a file from the device) 将其保存到其 ...

  2. setTimeout的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. jsp按钮隐藏自动点击

    <%@ page language="java" import="java.util.*" pageEncoding="big5"%& ...

  4. JPA @MappedSuperclass注解的使用说明

    基于代码复用和模型分离的思想,在项目开发中使用JPA的@MappedSuperclass注解将实体类的多个属性分别封装到不同的非实体类中. 1.@MappedSuperclass注解只能标准在类上:@ ...

  5. 单点登录实现----CAS(一)

    最近我们部门交接了一个新项目--- passport,即我司的单点登录系统,虽然没有交接给我,但是个人觉得登录技术是个很好的知识,于是就忙里偷闲简单地学习了下. 单点登录SSO(single sign ...

  6. oracle表分区详解(按天、按月、按年等)

    分区表的概念:  当表中的数据量不断增大,查询数据的速度就会变慢,应用程序的性能就会下降,这时就应该考虑对表进行分区.表进行分区后,逻辑上表仍然是一张完整的表,只是将表中的数据在物理上存放到多个表空间 ...

  7. JSON.stringify 在OA差旅中转换为字符串传给后端,(使用from表单的形式)

    $('#bookForm').find('#formData').val(transInfo.fromData).end().submit(); 这里的val()中的transInfo.fromDat ...

  8. gzip压缩及测试方法【转载】

    Nginx开启Gzip压缩大幅提高页面加载速度 http://www.veryhuo.com/a/view/51706.html 刚刚给博客加了一个500px相册插件,lightbox引入了很多js文 ...

  9. sql server 条件 not in (null)总是false

    SELECT  1 WHERE   2 NOT  IN ( 1 ); 结果: (无列名) 1   SELECT  1 WHERE   2 NOT  IN ( 1, NULL ); 结果:(无列名)   ...

  10. SDDC-SDN-SDS

    SDDCSDNSDS软件定义存储是一个较大的行业发展趋势,这个行业还包括软件定义网络(SDN)和软件定义数据中心(SDDC). SDDC依赖于虚拟化和云计算技术, SDDC的目标是虚拟化数据中心的一切 ...