BZOJ 3224 Treap
部分还没调到满意的程度,效率比splay略好
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+11;
unsigned int SEED = 17;
inline int Rand(){
SEED=SEED*1103515245+12345;
return SEED/65536;
}
struct TP{
int son[maxn][2],val[maxn],fix[maxn],size[maxn];
int tot,root;
#define lc son[o][0]
#define rc son[o][1]
void init(){
root=0;
tot=1;
son[0][0]=son[0][1]=val[0]=fix[0]=size[0]=0;
}
void pu(int o){
size[o]=1+size[lc]+size[rc];
}
int node(int v){
son[tot][0]=son[tot][1]=0;
size[tot]=1;
val[tot]=v;
fix[tot]=Rand();
return tot++;
}
int merge(int a,int b){
if(!a)return b;if(!b)return a;
if(fix[a]<fix[b]){
son[a][1]=merge(son[a][1],b);
pu(a);
return a;
}
else{
son[b][0]=merge(a,son[b][0]);
pu(b);
return b;
}
}
void split(int o,int k,int &a,int &b){
if(!o) a=b=0;
else{
if(val[o]<=k){
a=o;
split(rc,k,rc,b);
}
else{
b=o;
split(lc,k,a,lc);
}
pu(o);
}
}
int kth(int o,int k){
while(1){
if(k<=size[lc]) o=lc;
else if(k==size[lc]+1) return o;
else{k-=size[lc]+1;o=rc;}
}
}
////
void insert(int o,int v){
int a,b;
split(root,v,a,b);// if no root
root=merge(merge(a,node(v)),b);
}
void del(int o,int k){
int a,b,c;
split(root,k,a,c);
split(a,k-1,a,b);
b=merge(son[b][0],son[b][1]);
root=merge(merge(a,b),c);
}
int krank(int k){
int a,b;
split(root,k-1,a,b);
int ans=size[a]+1;
root=merge(a,b);
return ans;
}
int rankk(int k){
return val[kth(root,k)];
}
int pre(int k){
int a,b;
split(root,k-1,a,b);
int ans=val[kth(a,size[a])];//
root=merge(a,b);
return ans;
}
int succ(int k){
int a,b;
split(root,k,a,b);
int ans=val[kth(b,1)];
root=merge(a,b);
return ans;
}
}tp;
int main(){
int n,op,x,a,b,c;
while(scanf("%d",&n)^-1){
tp.init();
for(int i = 1; i <= n;i++){
scanf("%d%d",&op,&x);
if(op==1) tp.insert(tp.root,x);
if(op==2) tp.del(tp.root,x);
if(op==3) printf("%d\n",tp.krank(x));
if(op==4) printf("%d\n",tp.rankk(x));
if(op==5) printf("%d\n",tp.pre(x));
if(op==6) printf("%d\n",tp.succ(x));
}
}
return 0;
}
BZOJ 3224 Treap的更多相关文章
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- BZOJ 3224 - 普通平衡树 - [Treap][Splay]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...
- [bzoj 3224]手写treap
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 bzoj不能用time(0),看到这个博客才知道,我也RE了好几发…… #inclu ...
- BZOJ 3224: Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
- BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)
题目链接 treap及树状数组模板题. treap版: #include<bits/stdc++.h> using namespace std; typedef long long ll; ...
- BZOJ - 3224 可持久化Treap 树形操作
这个题目去年就做过了,这次稍微改了一下 都是基础操作 #include<iostream> #include<algorithm> #include<cstdio> ...
- bzoj 3224: Tyvj 1728 普通平衡树【非旋treap】
就是非旋treap的板子 #include<iostream> #include<cstdio> #include<cstdlib> using namespace ...
随机推荐
- 面试题:缓存Redis与Memcached的比较 有用
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度. Memca ...
- sql server 错误总结
1>无法访问sql server2000数据库 1.1>安装sql server2000 sp1的补丁包. 1.2>sql server 数据库开启了允许远程访问. 1.3>s ...
- FlyweightPattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...
- c# 二分查找法(2分钟算法)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 为docker设置国内镜像
docker的默认镜像(https://hub.docker.com/)地址,拉取镜像时是比较慢的,经常会超时,有时拉取几个小时.为了加快拉取的时间和速度,需要添加中国的镜像地址: 国内的加速地址: ...
- APP压力稳定性测试
转自:https://www.cnblogs.com/nuonuozhou/p/8643735.html 1.android系统自带monkey程序,模拟用户触摸屏幕,滑动track ball,按键等 ...
- ORCHARD学习教程-介绍
ORCHARD 是什么? Orchard 是由微软公司创建,基于 ASP.NET MVC 技术的免费开源内容管理系统: 可用于建设博客.新闻门户.企业门户.行业网站门户等各种网站 简单易用的后台界面 ...
- BZOJ 2301 Problem b(莫比乌斯反演+分块优化)
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- 个人项目:wc程序(java)
Github项目地址:https://github.com/jat0824/wc.git 项目相关要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行 ...
- 【[APIO/CTSC2007]动物园】状压DP
题目测评:https://www.luogu.org/problemnew/show/P3622 题目描述 新建的圆形动物园是亚太地区的骄傲.圆形动物园坐落于太平洋的一个小岛上,包含一大圈围栏,每个围 ...