P3369 【模板】普通平衡树(Treap/SBT)(pb_ds版)

时间:2022-05-08
本文章向大家介绍P3369 【模板】普通平衡树(Treap/SBT)(pb_ds版),主要内容包括题目描述、输入输出格式、输入输出样例、说明、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

题目描述

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

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

输入输出格式

输入格式:

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

输出格式:

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

输入输出样例

输入样例#1:

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

输出样例#1:

106465
84185
492737

说明

时空限制:1000ms,128M

1.n的数据范围:n<=100000

2.每个数的数据范围:[-1e7,1e7]

来源:Tyvj1728 原名:普通平衡树

在此鸣谢

颓废的时候闲来无事研究一下pb_ds,,

想象一下在考场上别人花n个小时写平衡树调平衡树,而你五分钟就秒了的快感

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<ext/pb_ds/tree_policy.hpp>
 6 #include<ext/pb_ds/assoc_container.hpp>
 7 #include<algorithm>
 8 #define lli long long 
 9 using namespace std;
10 using namespace __gnu_pbds;
11 void read(lli &n)
12 {
13     char c='+';lli x=0;bool flag=0;
14     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
15     while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
16     flag==1?n=-x:n=x;
17 }
18 tree<lli,null_type,std::less<lli>,splay_tree_tag,tree_order_statistics_node_update>st;
19 int main()
20 {
21     ios::sync_with_stdio(0);
22     lli T;
23     lli ans,x,y;
24     cin>>T;
25     for(lli i=1;i<=T;i++)
26     {
27         
28         //read(x);read(y);
29         cin>>x>>y;
30         if(x==1) st.insert((y<<20)+i);
31         else if(x==2)st.erase(st.lower_bound(y<<20));
32         else if(x==3)printf("%lldn",st.order_of_key(y<<20)+1);
33         else
34         {
35             if(x==4)ans=*st.find_by_order(y-1);
36             if(x==5)ans=*--st.lower_bound(y<<20);
37             if(x==6)ans=*st.lower_bound((y+1)<<20);
38             printf("%lldn",ans>>20);
39         }
40     }
41     return 0;
42 }