TOJ5705动态序列操作(STL or treap)
传送门:动态序列操作
在一个动态变化的序列中,完成以下基本操作:
(1)插入一个整数
(2)删除一个整数
(3)查找序列中最大的数
(4)查找序列中最小的数
(5)求x的前驱(前驱定义为不大于x的序列中最大的数)
(6)求x的后继(后继定义为不小于x的序列中最小的数)
输入
第一行为n,表示操作的次数,接下来有n行(n≤100000)
每行为以下操作之一:
(1)1 x:将x插入
(2)2 x:删除x,x在序列中一定存在,若有多个相同值,只删除一个。
(3)3:查找序列中最大的数
(4)4:查找序列中最小的数
(5)5 x:求x的前驱
(6)6 x:求x的后继
数据保证结果一定存在,其中序列中整数的绝对值不超过107。
输出
对于op取3、4、5、6等操作,输出相应的结果。
样例输入
10
1 2
3
1 5
4
1 10
2 5
1 3
6 8
1 11
5 3
样例输出
2
2
10
3
思路:
瞎搞:STL vector or 树状数组二分
正解:treap or splay(我也不会)
说说vector瞎搞吧,刚开始用set维护,需要另外有一个Map存是否删完了,于是光荣超内存了。扔了几天回头拿vector写了一发过了。
主要操作只有 insert:插入 earse:删除 , upper_bound ,lower_bound 两个对vector的二分查找。
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址
这里只需要注意,“不大于”,“不小于”这几个字眼即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 2000000000
#define eps 1e-8
#define pi 3.141592653589793
vector<int>v;
int main()
{
int _;
for(scanf("%d",&_);_--;){
int cmd,x;
scanf("%d",&cmd);
if(cmd == ){
scanf("%d",&x);
v.insert(lower_bound(v.begin(),v.end(),x),x);
}else if(cmd == ){
scanf("%d",&x);
v.erase(lower_bound(v.begin(),v.end(),x));
}else if(cmd == ){
printf("%d\n",v[v.size()-]);
}else if(cmd == ){
printf("%d\n",v[]);
}else if(cmd == ){
scanf("%d",&x);
vector<int>::iterator it = lower_bound(v.begin(),v.end(),x);
if((*it) == x)
printf("%d\n",x);
else
printf("%d\n",*(--it));
}
else if(cmd == ){
scanf("%d",&x);
vector<int>::iterator it = upper_bound(v.begin(),v.end(),x);
vector<int>::iterator itt = it;
if(*(--itt) == x && itt!=v.begin())
printf("%d\n",x);
else
printf("%d\n",*it);
}
/*---test---*/
int f = ;
if(f == ){
printf("zz :");
for(int i = ; i < v.size() ; i ++)
printf("%d ",v[i]);
puts("");
}
/*---test---*/
}
return ;
}/*
10
1 2
3
1 5
4
1 10
2 5
1 3
6 8
1 11
5 3 144
1 1
2 1
1 1
1 3
2 3
1 3
1 10
2 10
1 10
1 10
1 11
6 10
6 10
5 10
6 9
6 -100
5 123123
*/
600MS暴力做法
如果学完树状数组上二分,会更新一下树状数组二分的求解。
TOJ5705动态序列操作(STL or treap)的更多相关文章
- NOIP 模拟 序列操作 - 无旋treap
题意: 一开始有n个非负整数h[i],接下来会进行m次操作,第i次会给出一个数c[i],要求选出c[i]个大于0的数并将它们-1,问最多可以进行多少次? 分析: 首先一个显然的贪心就是每次都将最大的c ...
- Python通用序列操作
1.序列概览 1.数据结构 序列.容器 Python中最基本的数据结构是序列,其有索引(从左到右第一个索引为0,从右到左第一个索引为-1). Python包含6中内建的序列: 列表 元组 字符串 Un ...
- 【BZOJ-2962】序列操作 线段树 + 区间卷积
2962: 序列操作 Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 678 Solved: 246[Submit][Status][Discuss] ...
- 【BZOJ-1858】序列操作 线段树
1858: [Scoi2010]序列操作 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1961 Solved: 991[Submit][Status ...
- bzoj 1858: [Scoi2010]序列操作
1858: [Scoi2010]序列操作 Time Limit: 10 Sec Memory Limit: 64 MB 线段树,对于每个区间需要分别维护左右和中间的1和0连续个数,并在op=4时特殊 ...
- BZOJ 1858: [Scoi2010]序列操作( 线段树 )
略恶心的线段树...不过只要弄清楚了AC应该不难.... ---------------------------------------------------------------- #inclu ...
- .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper
.NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper 參考演示样例代码,例如以下所看到的: /// <summary> /// MySql ...
- [bzoj]2962序列操作
[bzoj]2962序列操作 标签: 线段树 题目链接 题意 给你一串序列,要你维护三个操作: 1.区间加法 2.区间取相反数 3.区间内任意选k个数相乘的积 题解 第三个操作看起来一脸懵逼啊. 其实 ...
- bzoj 2962 序列操作
2962: 序列操作 Time Limit: 50 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 有一个长度为n的序列, ...
随机推荐
- day25 CMDB(1)
CMDB项目介绍 参考地址: http://www.cnblogs.com/wupeiqi/articles/4556300.html http://www.cnblogs.com/wupeiqi/a ...
- laravel 博客项目部署到Linux系统后报错 权限都设置为777,仍然报错没有权限
阿里工程师最后给出解决方案.
- 设置PHP最长运行时间
通常来说,默认的PHP程序最大运行时间是30s,如果你的程序运行超过这个时间限制,那么会有类似Maximum execution time of 30 seconds exceeded的报错. 有几种 ...
- ELK练习
1.ELK练习 PUT s3/_doc/ { "mappings" : { "doc" : { "properties" : { " ...
- 02docker简单使用和配置(网络、存储和Hub)
四:网络 1:命名容器 在各种docker命令中,可以通过名字中找到对应的容器.之前创建的容器都是由docker自动命名的,可以在docker run中,通过--name参数指定容器的名字.比如: ...
- javascript中json对象与字符串互转及取值
一. json字符串转换为javascript对象,并取值 var answer = '{"id":0}' var value= JSON.parse(answer); //转 ...
- Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作
我们做listview左右滑动操作时,一般中情况下,都是像QQ那样,左滑弹出操作菜单(删除.编辑),然后选择菜单操作: 这样的效果不可谓不好,算是非常经典. 另外,有少数的APP,尤其是任务管理类的A ...
- Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(一)
一.Creating a trace or workload 注意点: In the Trace Properties dialog box, there is a checkbox option i ...
- oracle函数 NLS_UPPER(x[,y])
[功能]返回字符串并将字符串的转换为大写; [参数]x字符型表达式 [参数]Nls_param可选,指定排序的方式(nls_sort=) . SCHINESE_RADICAL_M(部首.笔画) SCH ...
- laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题
https://stackoverflow.com/questions/48568739/unable-to-open-file-for-reading-swift-ioexception-in-la ...