这个题目去年就做过了,这次稍微改了一下

都是基础操作

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 2e5+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7;
unsigned int SEED = 19260817;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline int Rand(){
SEED=SEED*1103515245+12345;
return SEED/65536;
}
struct Treap{
int son[MAXN][2],root,tot;
int val[MAXN],fix[MAXN],size[MAXN];
#define lc son[o][0]
#define rc son[o][1]
void init(){
root=0;
son[0][0]=son[0][1]=0;
val[0]=fix[0]=size[0]=0;
tot=1;
}
int node(int v){
son[tot][0]=son[tot][1]=0;
val[tot]=v; fix[tot]=Rand();
size[tot]=1;
return tot++;
}
void pu(int o){
size[o]=size[lc]+size[rc]+1;
}
void split(int o,int pivot,int &a,int &b){
if(!o){
a=b=0;
return;
}else if(val[o]>pivot){
b=o;
split(lc,pivot,a,lc);
pu(o);
}else{
a=o;
split(rc,pivot,rc,b);
pu(o);
}
}
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 insert(int v){
int a,b,t=node(v);
split(root,v,a,b);
root=merge(merge(a,t),b);
}
void del(int x){
int a,b,c,d;
split(root,x,a,b);
split(a,x-1,c,d);
d=merge(son[d][0],son[d][1]);//d的根不要了
root=merge(merge(c,d),b);
}
int krank(int k){
int a,b;
split(root,k-1,a,b);
int res=size[a]+1; //最小的相同数必然是恰比k-1子树规模大
root=merge(a,b);
return res;
}
int kth(int k){
int o=root;
while(1){
if(k<=size[lc]){
o=lc;
}else if(k==size[lc]+1){
return o;
}else{
k-=size[lc]+1;
o=rc;
}
}
}
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;
}
}
}
int pre(int x){
int a,b;
split(root,x-1,a,b);
int t=kth(a,size[a]);
root=merge(a,b);
return t;
}
int succ(int x){
int a,b;
split(root,x,a,b);
int t=kth(b,1);
root=merge(a,b);
return t;
}
}tp;
int n,m,a[MAXN];
int main(){
while(cin>>n){
tp.init();
rep(i,1,n){
int op=read();
int x=read();
switch(op){
case 1:tp.insert(x);break;
case 2:tp.del(x);break;
case 3:println(tp.krank(x));break;
case 4:println(tp.val[tp.kth(x)]);break;
case 5:println(tp.val[tp.pre(x)]);break;
case 6:println(tp.val[tp.succ(x)]);break;
}
}
}
return 0;
}

BZOJ - 3224 可持久化Treap 树形操作的更多相关文章

  1. Codeforces - 675D 可持久化Treap 树形操作

    题意:模拟二叉树的构造过程,给出\(n\)个节点,每次从根插入,小于当前节点转到左儿子,否则右儿子,输出第\([2,n]\)个节点的父亲的权值 直接手动模拟会被链式结构T掉 网上找了下发现二叉树的性质 ...

  2. Codeforces - 38G 可持久化Treap 区间操作

    题意:\(n\)个人排队,每个人有重要度\(p\)和不要脸度\(c\),如果第\(i\)个人的重要度大于第\(i-1\)个人的重要度,那么他们之间可以交换,不要脸度-1,交换后先前的第\(i\)个人也 ...

  3. BZOJ 3224 - 普通平衡树 - [Treap][Splay]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...

  4. [bzoj 3224]手写treap

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 bzoj不能用time(0),看到这个博客才知道,我也RE了好几发…… #inclu ...

  5. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  6. BZOJ 3224 普通平衡树(Treap模板题)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14301  Solved: 6208 [Submit][ ...

  7. BZOJ 3595: [Scoi2014]方伯伯的Oj SBT+可持久化Treap

    3595: [Scoi2014]方伯伯的Oj Time Limit: 6 Sec  Memory Limit: 256 MBSubmit: 102  Solved: 54[Submit][Status ...

  8. 可持久化Treap 赛前摸鱼笔记

    1.基本结构 随机化工具 unsigned int SEED = 19260817; //+1s inline int Rand(){ SEED=SEED*1103515245+12345; retu ...

  9. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. Spring Boot 启动失败,描述/Description: Cannot determine embedded database driver class for database type NONE

    异常截图====> 快速解决方式==> 在SpringBoot的启动类上面添加注解:@EnableAutoConfiguration(exclude={DataSourceAutoConf ...

  2. CopyOnWriteArrayList原理

    http://blog.csdn.net/chayangdz/article/details/76347465 总结的很到位: http://www.cnblogs.com/java-zhao/p/5 ...

  3. Codeforces 427E Police Patrol

    找中间的数,然后从两头取. #include<stdio.h> ; int pos[MAX]; int main() { int n,m,tmp; int i; int pol; long ...

  4. Socket接口原理及用C#语言实现

    首先从原理上解释一下采用Socket接口的网络通讯,这里以最常用的C/S模式作为范例,首先,服务端有一个进程(或多个进程)在指定的端口等待客户来连接,服务程序等待客户的连接信息,一旦连接上之后,就可以 ...

  5. ubuntu 14.04编译安装xen4.4总结

    1. 安装环境 操作系统:ubuntu14.04 xen版本:xen4.4 2. 依赖包的安装 在安装xen之前先进行依赖包的安装,在不停得尝试之后,总结出以下需要安装的依赖包. sudo apt-g ...

  6. easyui-dialog 弹窗

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

  7. MQ入门总结(一)消息队列概念和使用场景

    一.消息队列 消息即是信息的载体.为了让消息发送者和消息接收者都能够明白消息所承载的信息(消息发送者需要知道如何构造消息:消息接收者需要知道如何解析消息),它们就需要按照一种统一的格式描述消息,这种统 ...

  8. Vue 动态传值,Get传值

    Vue 路由get传值1.动态传值 1.1需要在路由配置时指定参数: {component:'/home/:id'} 1.2在routerlink中指定格式:<router-link :to=& ...

  9. ### 20165219 2017-2018-2《Java程序设计》结对编程一 第二周总结

    20165219 2017-2018-2<Java程序设计>结对编程一 第二周总结 结对对象 20165219王彦博 20165232何彦达 需求分析 实现一个程序,要求: 1 支持整数运 ...

  10. js 封装常用方法

    1. 获取数据类型 function getType(params) { , -) } 2. 深拷贝 function deepCopy(params) { var obj; if (typeof p ...