bzoj 1861 treap
思路:搞搞平衡树。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define y1 skldjfskldjg #define y2 skldfjsklejg using namespace std;const int N = 4e5 + 7; const int M = 5e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 45989; const int B = 1e5;int val[N], hs[N], n, m; char s[10]; struct node {node* ch[2];int key, fix, sz, cnt;void update() {sz = ch[0]->sz + ch[1]->sz + cnt;} };typedef node* P_node;struct Treap {node base[N], nil;P_node root, null, len;Treap() {root = null = &nil;null->key = null->fix = 1e9;null->sz = null->cnt = 0;null->ch[0] = null->ch[1] = null;len = base;}P_node newnode(int tkey) {len->key = tkey;len->fix = rand();len->ch[0] = len->ch[1] = null;len->sz = len->cnt = 1;return len++;}void rot(P_node &p, int d) {P_node k = p->ch[d ^ 1];p->ch[d ^ 1] = k->ch[d];k->ch[d] = p;p->update();k->update();p = k;}void _Insert(P_node &p, int tkey) {if(p == null) {p = newnode(tkey);} else if(p->key == tkey) {p->cnt++;} else {int d = tkey > p->key;_Insert(p->ch[d], tkey);if(p->ch[d]->fix > p->fix) {rot(p, d ^ 1);}}p->update();}void _Delete(P_node &p, int tkey) {if(p == null) return;if(p->key == tkey) {if(p->cnt > 1) p->cnt--;else if(p->ch[0] == null) p = p->ch[1];else if(p->ch[1] == null) p = p->ch[0];else {int d = p->ch[0]->fix > p->ch[1]->fix;rot(p, d);_Delete(p->ch[d], tkey);}} else {_Delete(p->ch[tkey > p->key], tkey);}p->update();}int _Kth(P_node p, int k) {if(p == null || k < 1 || k > p->sz) return 0;if(k < p->ch[0]->sz + 1) return _Kth(p->ch[0], k);if(k > p->ch[0]->sz + p->cnt) return _Kth(p->ch[1], k - p->ch[0]->sz - p->cnt);return p->key;}int _Rank(P_node p, int tkey, int res) {if(p == null) return -1;if(p->key == tkey) return p->ch[0]->sz + res + 1;if(tkey < p->key) return _Rank(p->ch[0], tkey, res);return _Rank(p->ch[1], tkey, res + p->ch[0]->sz + p->cnt);}int _Pred(P_node p, int tkey){if(p == null) return -1e9;if(tkey <= p->key) return _Pred(p->ch[0], tkey);return max(p->key, _Pred(p->ch[1], tkey));}int _Succ(P_node p, int tkey){if(p == null) return 1e9;if(tkey >= p->key) return _Succ(p->ch[1], tkey);return min(p->key, _Succ(p->ch[0], tkey));}void Insert(int tkey){ _Insert(root,tkey); }void Delete(int tkey){ _Delete(root,tkey); }int Kth(int k){ return _Kth(root,k); }int Rank(int tkey){ return _Rank(root,tkey,0); }int Pred(int tkey){ return _Pred(root,tkey); }int Succ(int tkey){ return _Succ(root,tkey); } }tp; int main() {scanf("%d%d", &n, &m);int mx = n + B, mn = 1 + B;for(int i = 1; i <= n; i++) {int x; scanf("%d", &x);val[x] = i + B;hs[i + B] = x;tp.Insert(i + B);}while(m--) {int id, t;scanf("%s", s);if(s[0] == 'T') {scanf("%d", &id);tp.Delete(val[id]);val[id] = --mn;hs[mn] = id;tp.Insert(val[id]);} else if(s[0] == 'B') {scanf("%d", &id);tp.Delete(val[id]);val[id] = ++mx;hs[mx] = id;tp.Insert(val[id]);} else if(s[0] == 'I') {scanf("%d%d", &id, &t);if(t == 1) {int z = tp.Succ(val[id]);if(z < -100000000 || z > 100000000) continue;int id2 = hs[z], v = val[id];swap(val[id], val[id2]);swap(hs[v], hs[z]);} else if(t == -1) {int z = tp.Pred(val[id]);if(z < -100000000 || z > 100000000) continue;int id2 = hs[z], v = val[id];swap(val[id], val[id2]);swap(hs[v], hs[z]);}} else if(s[0] == 'A'){scanf("%d", &id);printf("%d\n", tp.Rank(val[id]) - 1);} else {scanf("%d", &id);printf("%d\n", hs[tp.Kth(id)]);}}return 0; }
转载于:https://www.cnblogs.com/CJLHY/p/9600126.html
bzoj 1861 treap相关推荐
- BZOJ 1503 treap
思路: treap (算是基本操作吧-..) 加减的操作数很少 就暴力好啦 每回判断一下最小的数是不是比M小 如果是 就删,继续判断 搞定. //By SiriusRen #include <c ...
- BZOJ 3544 treap (set)
我只是想找个treap的练习题-- 每回找到lower_bound 就好啦 //By SiriusRen #include <cstdio> #include <cstring> ...
- splay伸展树基础操作(bzoj 1861: [Zjoi2006]Book 书架)
splay:一种排序树(中序遍历权值有序) 主要性质:随着访问翻转次数的增多,复杂度越来越接近logn,形态也越来越接近平衡树 主要功能:每次将要询问or删除or修改的点先一路翻转到根,然后再满足所需 ...
- BZOJ 3224 Treap
部分还没调到满意的程度,效率比splay略好 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; u ...
- BZOJ 1861 ZJOI2006 Book 书架 Splay
题目大意:--自己看懒得打了 很裸的Splay 首先开一个指针数组记录每个值代表的节点 然后就能找到某本书在序列中的什么位置了 总感觉这题可以不用Splay的说--一定是我的错觉 样例中居然尼玛有中文 ...
- BZOJ 3224: Tyvj 1728 普通平衡树【Treap】
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一些数 ...
- BZOJ 1208: [HNOI2004]宠物收养所 (Treap)
BZOJ 1208: [HNOI2004]宠物收养所 题目概述: 有一家宠物收养所,提供两种服务:收养主人遗弃的宠物和让新主人领养宠物. 宠物收养所中总是会有两种情况发生:遗弃宠物过多和领养宠物人过多 ...
- BZOJ 1503: [NOI2004]郁闷的出纳员 Treap
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- BZOJ 3435: [Wc2014]紫荆花之恋 【(替罪羊树式)动态点分治 + Treap】
BZOJ 传送门 洛谷传送门 题目分析: 似乎做过几道点分树的题之后题解还是比较好懂的. 这位dalao的题目分析非常的到位,Orz. PoPoQQQ的具体解读也非常的清晰,Orz. R i + R ...
- BZOJ 1901 Dynamic Rankings(线段树+treap)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1901 题意:给出一个数列,两种操作:(1)询问区间第K小值:(2)修改某个位置的值. 思 ...
最新文章
- 个人信息泄露致电信诈骗猖獗 专家:治理亟须完善立法
- linux redis WARNING overcommit_memory is set to 0! 解决方案
- c语言程序设计家庭收支类,家庭支出管理系统—c语言程序设计
- 使用调用者权限实现Schema导向操作
- C++11线程管理基础
- 45度做人 90度做事 180度为人 360度处事
- 14种方法助你参与开源项目
- 深度linux缺点,原来国产深度系统有这些“缺陷”,难怪只有少数人在使用!
- Android的面孔_Actiyity
- Windows下断言的类型及实现
- Installing specific major Java JDK versions on OS X via Homebrew
- react native在static中使用this方法
- 基础之 window-self-top-opener
- 多行文字省略(涵盖标点符号,中英文等复杂字符串)
- 轮询、长轮询、长连接、websocket
- mysql-front服务器_mysql-front远程连接自己linux服务器上的mysql服务器
- java 上传图片 并压缩图片大小
- win10在哪打开ie浏览器?windows11怎么打开ie浏览器?
- 把网页知乎的视频下载下来
- 非常详细的Docker学习教程
热门文章
- liunx中常用命令 -大数据
- gitblit git SERVER window 安装配置 hook post-receive 自动部署
- 阶段3 2.Spring_10.Spring中事务控制_11 spring5新特性的介绍
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_2_字符输入流读取字符数据...
- HDU - 5950 Recursive sequence(矩阵快速幂)
- MVN TEST指定运行脚本
- jQuery课堂测验
- QT修改应用程序图标
- CF 71C. Round Table Knights