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 = 100100; int n,q,a,b; set<int>s; int solve() { while(b <= n) { if(s.find(b) != s.end()) b++; else return b; } return -1; } int main() { scanf("%d%d",&n,&q); for(int i = 0; i < q; i++) { scanf("%d%d",&a,&b); if(a == 1) { s.insert(b); } else { printf("%d\n",solve()); // 也可以s.count判断是否存在。 /*while(s.count(b)) { b++; } printf("%d\n" , b);*/ } } return 0; }
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 = 100100; int n,q,a,b; int main() { unordered_map<int , int>mp; scanf("%d%d",&n,&q); for(int i = 0; i < q; i++) { scanf("%d%d",&a,&b); if(a == 1) { mp[b] = -1 ; } else { while(mp[b] == -1) b++ ; printf("%d\n" , b); } } return 0; }