stl (set or 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 g n g

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^91x<n<109, 1≤q<1061 \leq q<10^6 1q<106 and zzz is 111 or 222

Output

Output the answer for each query.

Sample input

5 3
1 2
2 2
2 1

Sample Output

. 3 
a 

title intended to: give 1-n, q operations
the first operation (1): a value marked as unavailable;
second operation (2): a useful output of the first value after the value.
Magic compiler: c ++ 14 timeout, c ++ 11 pass by Liao.

#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 ());
             // may determine whether there is s.count. 
            / * The while (s.count (B)) 
            { 
                B ++; 
            } 
            the printf ( "% D \ n-", B); * / 

        } 
    } 
    return  0 ; 
}

map time out to use 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;
}

 

 



 

Guess you like

Origin www.cnblogs.com/nonames/p/11488306.html