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 ...
随机推荐
- MySQL新增用户及赋予权限
创建用户 USE mysql; #创建用户需要操作 mysql 表 # 语法格式为 [@'host'] host 为 'localhost' 表示本地登录用户,host 为 IP地址或 IP 地址区间 ...
- LOJ2320「清华集训 2017」生成树计数
由于菜鸡的我实在是没学会上升幂下降幂那一套理论,这里用的是完全普通多项式的做法. 要是有大佬愿意给我讲讲上升幂下降幂那一套东西,不胜感激orz! 首先可以想到prufer序列,如果不会的话可以左转百度 ...
- 配置apache运行cgi程序
配置apache运行cgi程序 文章目录 [隐藏] ScriptAlias目录的CGI ScriptAlias目录以外的CGI 配置apache运行cgi程序可分为两种情况,一是ScriptAlias ...
- maven打包的时候you are running on a JRE rather than a JDK?
解决方案.删除掉,然后重新添加. 然后remove掉 然后Add Library
- android 开发架构学习
Android DataBinding(数据绑定)入门与实战 http://examplecode.cn/2018/07/20/android-databinding-01-introduction/ ...
- php array_pop()函数 语法
php array_pop()函数 语法 作用:删除数组中的最后一个元素.博智达 语法:array_pop(array) 参数: 参数 描述 array 必需.规定数组. 说明:返回数组的最后 ...
- 可恶!学了这么久的LCA,联考的题目却是LCA+树形DP!!!可恶|!!!这几天想学学树形DP吧!先来一道入门题HDU 1520 Anniversary party
题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...
- luogu P1147 连续自然数和 x
P1147 连续自然数和 题目描述 对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M. 例子:1998+1999+2000+2001+2002 = 10000,所以 ...
- Go实现分布式外部排序
Go实现分布式外部排序 项目路径: https://github.com/Draymonders/go_external_sort 默认读入文件: small.in 默认输出文件:small.out ...
- int转字符串 stringstream
1. 设定一个任意数字串,数出这个数中的偶数个数,奇数个数,及这个数中所包含的所有位数的总数,将答案按 “偶-奇-总” 的位序,排出得到新数.重复进行,最后会得到 123. #include<i ...