设为首页 加入收藏

TOP

P3369 【模板】普通平衡树(Treap/SBT)(pb_ds版)
2017-10-11 18:34:28 】 浏览:10240
Tags: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("%lld\n",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("%lld\n",ans>>20);
39         }
40     }
41     return 0;
42 }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇BZOJ3295 CQOI2011 动态逆序对 下一篇Linux下使用Opencv打开笔记本摄像..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目