题目描述

这是一道模板题。

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

  1. 插入  x 数;
  2. 删除  x 数(若有多个相同的数,因只删除一个);
  3. 查询  x 数的排名(若有多个相同的数,因输出最小的排名);
  4. 查询排名为 x 的数;
  5. 求 x 的前趋(前趋定义为小于 x,且最大的数);
  6. 求 x 的后继(后继定义为大于 x,且最小的数)。

输入格式

第一行为 n,表示操作的个数,下面 n 行每行有两个数 opt 和 x, 表示操作的序号(1<=opt<=6)。

输出格式

对于操作 3、4、5、6 每行输出一个数,表示对应答案。

样例

样例输入

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

样例输出

106465
84185
492737

数据范围与提示

 1<=n<=105,-107<=x<=107
 ___________________________________________________________________________
 
fhq_treap模板
___________________________________________________________________________
  1 #include<bits/stdc++.h>
2 using namespace std;
3 const int maxn=1e5+10;
4 struct node
5 {
6 int val,lc,rc,siz,rd;
7 }tr[maxn];
8 int cnt,root,n;
9 int newnode(int v)
10 {
11 ++cnt;
12 tr[cnt].val=v;
13 tr[cnt].siz=1;
14 tr[cnt].rd=rand();
15 tr[cnt].lc=tr[cnt].rc=0;
16 return cnt;
17 }
18 void update(int cur)
19 {
20 tr[cur].siz=tr[tr[cur].lc].siz+tr[tr[cur].rc].siz+1;
21 }
22 int merge(int x,int y)
23 {
24 if(x*y==0)return x+y;
25 if(tr[x].rd<tr[y].rd)
26 {
27 tr[x].rc=merge(tr[x].rc,y);
28 update(x);
29 return x;
30 }
31 else
32 {
33 tr[y].lc=merge(x,tr[y].lc);
34 update(y);
35 return y;
36 }
37 }
38 void split(int cur,int v,int &x,int &y)
39 {
40 if(!cur)x=y=0;
41 else
42 {
43 if(tr[tr[cur].lc].siz+1<=v)
44 {
45 x=cur;
46 split(tr[cur].rc,v-tr[tr[cur].lc].siz-1,tr[cur].rc,y);
47 update(cur);
48 }
49 else
50 {
51 y=cur;
52 split(tr[cur].lc,v,x,tr[cur].lc);
53 update(cur);
54 }
55 }
56 }
57 void splitv(int cur,int v,int &x,int &y)
58 {
59 if(!cur)x=y=0;
60 else
61 {
62 if(tr[cur].val<=v)
63 {
64 x=cur;
65 splitv(tr[cur].rc,v,tr[cur].rc,y);
66 update(cur);
67 }
68 else
69 {
70 y=cur;
71 splitv(tr[cur].lc,v,x,tr[cur].lc);
72 update(cur);
73 }
74 }
75 }
76 void insert(int v)
77 {
78 int x,y;
79 splitv(root,v,x,y);
80 root=merge(merge(x,newnode(v)),y);
81 }
82 void del(int v)
83 {
84 int x,y,z;
85 splitv(root,v,x,z);
86 splitv(x,v-1,x,y);
87 y=merge(tr[y].lc,tr[y].rc);
88 root=merge(merge(x,y),z);
89 }
90 void find(int v)
91 {
92 int x,y;
93 splitv(root, v-1,x,y);
94 printf("%d\n",tr[x].siz+1);
95 root=merge(x,y);
96 }
97 void kth(int now,int v)
98 {
99 int cur=now;
100 if(v>tr[now].siz || v<1)return ;
101 while(cur)
102 {
103 if(tr[tr[cur].lc].siz+1==v)
104 {
105 printf("%d\n",tr[cur].val);
106 return ;
107 }
108 else if(tr[tr[cur].lc].siz >= v)cur=tr[cur].lc;
109 else
110 {
111 v-=tr[tr[cur].lc].siz+1;
112 cur=tr[cur].rc;
113 }
114 }
115 }
116 void pre(int v)
117 {
118 int x,y,z;
119 splitv(root,v-1,x,y);
120 kth(x,tr[x].siz);
121 root=merge(x,y);
122 }
123 void next(int v)
124 {
125 int x,y,z;
126 splitv(root,v,x,y);
127 kth(y,1);
128 root=merge(x,y);
129 }
130 int main()
131 {
132 scanf("%d",&n);
133 for(int op,x,i=0;i<n;++i)
134 {
135 scanf("%d%d",&op,&x);
136 if(op==1)insert(x);
137 else if(op==2)del(x);
138 else if(op==3)find(x);
139 else if(op==4)kth(root,x);
140 else if(op==5)pre(x);
141 else next(x);
142 }
143 return 0;
144 }

LOJ104 普通平衡树的更多相关文章

  1. BZOJ3224/LOJ104 普通平衡树 pb_ds库自带红黑树

    您需要写一种数据结构,来维护一些数,其中需要提供以下操作:1. 插入x2. 删除x(若有多个相同的数,因只删除一个)3. 查询x的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. ...

  2. BZOJ3224/LOJ104 普通平衡树 treap(树堆)

    您需要写一种数据结构,来维护一些数,其中需要提供以下操作:1. 插入x2. 删除x(若有多个相同的数,因只删除一个)3. 查询x的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. ...

  3. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  4. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  5. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  6. [普通平衡树treap]【学习笔记】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9046  Solved: 3840[Submit][Sta ...

  7. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

  8. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  9. 【Splay】bzoj3223-Tyvj1729文艺平衡树

    一.题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 ...

随机推荐

  1. sql字段拆分 ,连表子查询获取值

    1.连表子查询获取值 select bas.name,bas.id_card_num,bas.mobil_no,gender,bas.birthday,bas.height,bas.weight,pr ...

  2. ACID隔离性

    数据库ACID 一致性 原子性  隔离性  持久性 隔离性: 1.读未提交 2.读已提交 3.可重复读 4.串行 读未提交:容易引起脏读 读已提交:容易引起幻读(前后读到的行数不一致) 场景: A事务 ...

  3. Kubernetes项目简介

    Kubernetes项目简介 Kubernetes 是 Google 团队发起的开源项目,它的目标是管理跨多个主机的容器,提供基本的部署,维护以及运用伸缩,主要实现语言为 Go 语言.Kubernet ...

  4. spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingExcept ...

  5. 初学VBA

    一个最基本的VBA程序 Sub test() //宏开始 Dim ge As Range //定义变量 For Each ge In Range("a1:a10") //从a1到a ...

  6. wdCP V3.2

    wdCP是什么?关于wdCP更多的介绍,可看http://www.wdlinux.cn/wdcp/安装前先去体验下,看演示站吧http://www.wdlinux.cn/bbs/thread-5285 ...

  7. Petalinux和Vivado的安装

    Petalinux和Vivado的安装 背景 我是搞软件的, FPGA这块不太了解.由于机缘巧合,最近有接触到这块的开发.所以先挖一坑. 先声明我不是专业搞这块的,所以对这块的内容理解可能会有偏差,以 ...

  8. 基于 MPI/OpenMP 混合编程的大规模多体(N-Body)问题仿真实验

    完整代码: #include <iostream> #include <ctime> #include <mpi.h> #include <omp.h> ...

  9. 克隆slave

    在日常生活中,我们做的比较多的操作就是在线添加从库,比如线上有一主一丛两个数据库,由于业务的需要一台从库的读取量无法满足现在的需求,这样就需要我们在线添加从库,出于安全考虑,我们通常需要在从库上进行在 ...

  10. yum -y install gnuplot

    [root@test~]# yum -y install gnuplotLoaded plugins: fastestmirrorLoading mirror speeds from cached h ...