[hihoCoder1329] 带Split和Merge的Treap
题目链接: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的更多相关文章
- opencv —— split、merge 通道的分离与合并
对于三通道或四通道图像,有时要对某一通道的像素值进行修改或展示,这就需要进行通道分离操作.修改后,若要进行结果展示,就需要重新将各通道合并. 通道分离:split 函数 void split (Inp ...
- split()函数+merge()函数
在图像处理时,我们接触到的彩色以RGB居多,为了分析图像在某一通道上的特性,需要将图像的颜色通道进行分离,或者是在对某一颜色通道处理后重新进行融合.opencv提供了split()函数来进行颜色通道的 ...
- 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- ...
- 【数据结构】【平衡树】无旋转treap
最近在研究平衡树,看起来这种东西又丧水又很深,感觉很难搞清楚.在Ditoly学长的建议下,我先学习了正常的treap,个人感觉这应该是平衡树当中比较好懂的而且比较好写的一种. 然而,发现带旋treap ...
- 飞旋treap
虽然叫做非旋treap但是飞旋treap很带感所以就用这个名字了(SB) 这个东西是真的好写...... 主要的两个函数只有两个,rotate和splay,split和merge. merge就是大家 ...
- 非旋转Treap
Treap是一种平衡二叉树,同时也是一个堆.它既具有二叉查找树的性质,也具有堆的性质.在对数据的查找.插入.删除.求第k大等操作上具有期望O(log2n)的复杂度. Treap可以通过节点的旋 ...
- 平衡树合集(Treap,Splay,替罪羊,FHQ Treap)
今天翻了翻其他大佬的博客,发现自己有些...颓废... 有必要洗心革面,好好学习 序:正常的BST有可能退化,成为链,大大降低效率,所以有很多方法来保持左右size的平衡,本文将简单介绍Treap,S ...
- 洛谷-P3369-普通平衡树(Treap)
题目传送门 标题说平衡树,那么应该AVL,红黑树都能过,但是这次做这题主要是学习Treap,所以花了几天搞出了这题.其他方法以后再说吧 Treap(带旋转) #include <bits/std ...
- [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )
转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec Mem ...
随机推荐
- overflow:hidden清楚浮动的影响
在网页布局中有时会遇到这种情况: 如果左边用<dt>,右边用<dd>,放在一行显示,<dt>要设置float:left,这个应该都知道,问题是,第一行这样做没有问题 ...
- 关于超出部分隐藏加省略号的css方法
单行效果:display:block; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 多行效果:width: ...
- 2. Transcribing DNA into RNA
Problem An RNA string is a string formed from the alphabet containing 'A', 'C', 'G', and 'U'. Given ...
- ubuntu更新命令点点滴滴
ubuntu更新命令点点滴滴 一些非root的更新命令: sudo: sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一 ...
- 【转载】Restarting an analysis in ANSYS
原文地址:http://blog.sina.com.cn/s/blog_4b764640010168ru.html 这是自己最近做的一个例子,一是为了感谢okok论坛给与我的很大的帮助,二是起到抛 ...
- 移动端bug~~移动端:active伪类无效的解决方法【移动端 :active样式无效】
移动端:active伪类无效的解决方法[移动端 :active样式无效]2016-09-26 15:46:50 问题: 移动端开发的时候实现按钮的点击样式变化,但是在iphone[safiri Mo ...
- 【jq】c#零基础学习之路(5)自己编写简单的Mylist<T>
public class MyList<T> where T : IComparable { private T[] array; private int count; public My ...
- HTML 表格
HTML 表格:表格由<table>标签来定义,行数由<tr>标签来定义,单元格由<td>标签来定义:<table border="1"& ...
- UI崩溃的解决方案
在unity加载的时候主动强制关闭后,竟然ui崩溃,一直报错UnityEngine.UI.dll is in timestamps but is not known in assetdatabase. ...
- sql查询语句
//查询表的字段名和字段类型select column_name,data_type from information_schema.columns where table_name = '表名' / ...