So Easy(2019 ACM-ICPC 徐州赛区网络赛 B)

Problem Description

There are nn points in an array with index from 1 to n, and there are two operations to those points.

1: 1 x marking the point x is not available

2:2 x query for the index of the first available point after that point (including x itself) .

Input

n q
z1 ​x1​
...
zq ​xq​

q is the number of queries, z is the type of operations, and x is the index of operations. 1≤x<n<109,1≤q<106 and z is 1 or 2

Output

Output the answer for each query.

Sample Input

5 3
1 2
2 2
2 1

Sample Output

3
1

题意:给出一个数 n,代表存在一个从 1~n 的数列,再给出 m 个操作,每个操作为 1 x 或 2 x,1 x 代表将数列中的 x 数进行标记,2 x 代表查询数列中的 x,若 x 未被标记,就输出 x,若 x 已被标记,就查询其后面的数

思路:一个使用 map 即可以做的并查集裸题,但卡时间,利用 Hash 表或 unordered_map 均可解决

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

unordered_map<int,int> mp;
int f(int y) {
    if(y==-1)
        return -1;
    if(mp[y]==0)
        return y;
    return mp[y]=f(mp[y]);
}
int main() {
    int n,m;
    scanf("%d%d",&n,&m);

    mp[n]=-1;
    for(int i=1; i<=m; i++) {
        int x,y;
        scanf("%d%d",&x,&y);
        if(x==1)
            mp[y]=y+1;
        else
            printf("%d\n",f(y));
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值