The Preliminary Contest for ICPC Asia Xuzhou 2019 B. so easy 並查集

題意:1-n的序列,有兩個操作

1.刪除 x

2.查詢大於等於x的第一個沒有沒刪除的數

題解:需要反向插入,第一種辦法是直接插入UNordered map後,暴力找比x大的數

這種辦法如果按照數據範圍來出肯定t,數據水

第二種辦法是按照並查集的方法壓縮路徑

第二種正解

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mem(s) memset(s, 0, sizeof(s))
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e6+5;
const int mod = 998244353;
unordered_map<int,int> mp;
int Find(int x){
    if(!mp.count(x))return x;
    else return mp[x]=Find(mp[x]);
}
int main() 
{
    int n,q;
    scanf("%d%d",&n,&q);
    while(q--){
        int z,x;
        scanf("%d%d",&z,&x);
        if(z==1)
            mp[x]=Find(x+1);
        else {
            printf("%d\n",Find(x));
        }
    }
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章