The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

题意:
给[1,n],n个数,有两种操作:

1 x,删去x
2 x,查询还未被删去的数中大于等于x的最小的数是多少。

input:

5 3
1 2
2 2
2 1

output:

3
1

做法:按照并查集的方法压缩路径

代码:

#include<bits/stdc++.h>

using namespace std;
#define int long long 
unordered_map<int,int> mp;
int getf(int x){
    if(!mp.count(x))
        return x;
    else{
        return mp[x]=getf(mp[x]);
    }
}
signed main(){
    int n,q;
    cin>>n>>q;
    while(q--){
        int x,y;
        scanf("%lld%lld",&x,&y);
        if(x==1){
            mp[y]=getf(y+1);
        }else{
            printf("%lld\n",getf(y));
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/pengge666/p/11567631.html