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 ...
随机推荐
- Ubuntu18.04创建新的系统用户
目标: 1.为测试学习Docker,在虚拟机OS为18.04里,创建一个系统账号,账号名称:docker 2.在/home下有新建username的文件夹 一.建立账号 1.以root账号登录 2.u ...
- blockchain notes
1. IBM blockchain platform https://www.ibm.com/blockchain/platform/ 2. 开源项目hyperledger https://hyper ...
- Luogu 4951 [USACO 2001 OPEN]地震
水个博客玩. $01$分数规划. 题目要求$\frac{F - \sum_{i = 1}^{n}C_i}{T_i}$最大,设$\frac{F - \sum_{i}C_i}{T_i} \geq e$,移 ...
- [GO]copy的使用
package main import "fmt" func main() { srcslice := [],} dstslice := [],,,,,} copy(dstslic ...
- linq to sql 实现左(右)连接,那个方法是对的,该怎么处理
linq to sql 实现左(右)连接,那个方法是对的var query2 = from tb0 in db.table_0 join tb1 in db.table_1 on table_0.关 ...
- Recyclerview添加头布局和尾布局,点击效果
简介: 本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件 思路: 主要重写Recyclerview.Adapter中的一些方法 1.public int ...
- 求整数数组(长度为n),出现大于2/n次数的数字
条件:时间复杂度是O(n),空间复杂度是O(1) 方法1:标记法 , , , , , , , , , , , , , }; int len = arr.Length; int[] c = new in ...
- Spring中的用到的设计模式
应该说设计模式是我们在写代码时候的一种被承认的较好的模式.好的设计模式就像是给代码造了一个很好的骨架,在这个骨架里,你可以知道心在哪里,肺在哪里,因为大多数人都认识这样的骨架,就有了很好的传播性.这是 ...
- solidity_mapping_implementation
solidity 中 mapping 是如何存储的 为了探测 solidity mapping 如何实现,我构造了一个简单的合约. 先说结论,实际上 mapping的访问成本并不比直接访问storag ...
- 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)
七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...