stl(set或map)
https://nanti.jisuanke.com/t/41384
There are nnn points in an array with index from 111 to nnn, and there are two operations to those points.
1: 1 x1 \ x1 x marking the point xxx is not available
2: 2 x2 \ x2 x query for the index of the first available point after that point (including xxx itself) .
Input
nqn\quad qnq
z1x1z_1 \quad x_1z1x1
⋮\vdots⋮
zqxqz_q\quad x_qzqxq
qqq is the number of queries, zzz is the type of operations, and xxx is the index of operations. 1≤x<n<1091≤x<n<10^91≤x<n<109, 1≤q<1061 \leq q<10^6 1≤q<106 and zzz is 111 or 222
Output
Output the answer for each query.
样例输入
5 3
1 2
2 2
2 1
样例输出
3
1 题意:给出1-n,q个操作
第一个操作(1):将某个值标记为不可用;
第二个操作(2):输出该值以后的第一个有用值。
神奇的编译器:c++14超时,c++11就过辽。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std; #define mem(a) memset(a,0,sizeof(a))
#define ll long long
const int maxn = ;
int n,q,a,b;
set<int>s;
int solve()
{
while(b <= n)
{
if(s.find(b) != s.end())
b++;
else return b;
}
return -;
} int main()
{
scanf("%d%d",&n,&q);
for(int i = ; i < q; i++)
{
scanf("%d%d",&a,&b);
if(a == )
{
s.insert(b);
}
else
{
printf("%d\n",solve());
// 也可以s.count判断是否存在。
/*while(s.count(b))
{
b++;
}
printf("%d\n" , b);*/ }
}
return ;
}
map会超时要用unordered_map
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <unordered_map>
using namespace std; #define mem(a) memset(a,0,sizeof(a))
#define ll long long
const int maxn = ;
int n,q,a,b; int main()
{
unordered_map<int , int>mp;
scanf("%d%d",&n,&q);
for(int i = ; i < q; i++)
{
scanf("%d%d",&a,&b);
if(a == )
{
mp[b] = - ;
}
else
{
while(mp[b] == -) b++ ;
printf("%d\n" , b);
}
}
return ;
}
stl(set或map)的更多相关文章
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- STL中的map、unordered_map、hash_map
转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...
- [知识点]C++中STL容器之map
UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- STL中的map和unordered_map
STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...
- Linux环境下stl库使用(map)
例子1: testMap.cpp #include <string.h> #include <iostream> #include <map> #include & ...
- stl中的map数据类型
1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...
- 【STL学习】map&set
技术不只是我的工作,也是我的生活,以后的博客中会穿插一些个人的喜悦.愤怒或者感悟,希望大家能够接受. 我所有的一切,比我技术更好的怕是我的脸皮了,昨天收到京东面试没有通过的消息,喊了几句“我好悲伤啊” ...
- STL中关于map和set的四个问题?
STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 为何map和set的插入删除效率比用其他序列容器高? 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. ...
- C++标准模板库(STL)之Map
1.Map的常用用法 map:映射.可以将任何基本类型,结构体,STL容器映射到任何基本类型包括容器. 使用map,需要加map的头文件,#include<map>和using names ...
随机推荐
- LAMP架构编译安装过程详解
linux Git 安装 1.安装git依赖包 . yum install -y perl-ExtUtils-MakeMaker package . yum install -y tcl build- ...
- 绑定class -vue
1.值为对象 :class = "{ 'text-red': isActive }" data () { return { isActive : true } } :class = ...
- maven 查找依赖的办法
maven 你需的jar包名称 repository 比如我要做EJB,我要找jboss-j2ee.jar的Dependency 就在GOOGLE里输入 maven jboss-j2ee reposi ...
- 【leetcode】1095. Find in Mountain Array
题目如下: (This problem is an interactive problem.) You may recall that an array A is a mountain array i ...
- 【leetcode】493. Reverse Pairs
题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...
- 【leetcode】313. Super Ugly Number
题目如下: 解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值.解法大致如图: 代码如下: #include<map> #include<queue ...
- linux运维、架构之路-MySQL备份与恢复(四)
一.备份方式 ①逻辑备份(文件表示:SQL语句) ②物理备份(数据文件的二进制副本) ③基于快照的备份 ④基于复制的备份 二.备份工具 ①mysqldump:原生自带的逻辑备份工具 ②mysqlbin ...
- 前端面试题常考&必考之--盒子模型和box-sizing(项目中经常使用)
主要考察width的值,包括padding\border\content等属性??? box-sizing属性是css3特有的哦*** 1>当box-sizing:content-box;时,跟 ...
- mybatis添加数据时返回主键 insert 返回主键值
insert 返回主键值 useGeneratedKeys=“true” parameterType=“USer” keyProperty=“id”, <insert id="inse ...
- 计蒜客 T2237 魔法 分类讨论
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...