結果

問題 No.2220 Range Insert & Point Mex
ユーザー otoshigootoshigo
提出日時 2023-02-18 14:47:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 2,866 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 218,076 KB
最終ジャッジ日時 2025-02-10 18:50:52
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

template <class T>
struct RangeMex {
    const T inf = numeric_limits<T>::max();
    set<pair<T, T>> se;
    RangeMex() {
        se.insert(make_pair(-inf, -inf));
        se.insert(make_pair(inf, inf));
    }
    void add(T x) {
        auto id1 = se.lower_bound(make_pair(x + 1, x + 1));
        auto id2 = se.lower_bound(make_pair(x, x));
        id2--;
        pair<T, T> p1 = *id1, p2 = *id2;
        if (p1.first > x + 1 && p2.second < x - 1) {
            se.insert(make_pair(x, x));
        } else if (p1.first > x + 1) {
            se.insert(make_pair(p2.first, x));
            se.erase(p2);
        } else if (p2.second < x - 1) {
            se.insert(make_pair(x, p1.second));
            se.erase(p1);
        } else {
            se.insert(make_pair(p2.first, p1.second));
            se.erase(p1);
            se.erase(p2);
        }
    }
    void erase(T x) {
        auto id = se.lower_bound(make_pair(x + 1, x + 1));
        id--;
        pair<T, T> p = *id;
        if (p.second < x) return;
        if (p.first <= x - 1) se.insert(make_pair(p.first, x - 1));
        if (x + 1 <= p.second) se.insert(make_pair(x + 1, p.second));
        se.erase(p);
    }
    T mex(int l, int r) {  //[l,r)のmex
        --r;
        auto id = se.lower_bound(make_pair(l, l));
        pair<T, T> p = *id;
        if (p.first != l) {
            id--;
            pair<T, T> p2 = *id;
            if (p2.second < l) return l;
            else if (p2.second < r) return p2.second + 1;
            else return -1;
        } else if (p.second < r) return p.second + 1;
        else return -1;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    vector<tuple<int,int,int>>T;
    vector<int>A(n);
    rep(i,n){
        int l,r;
        cin>>l>>r>>A[i];
        T.push_back(make_tuple(l,-1,i));
        T.push_back(make_tuple(r,1,i));
    }
    int q;
    cin>>q;
    rep(i,q){
        int x;
        cin>>x;
        T.push_back(make_tuple(x,0,i));
    }
    sort(all(T));
    vector<int>ans(q);
    RangeMex<int>M;
    map<int,int>mp;
    int mex=0;
    for(auto[x,t,i]:T){
        if(t==0){
            ans[i]=M.mex(0,1<<30);
        }else if(t==-1){
            if(!mp.count(A[i]))M.add(A[i]);
            mp[A[i]]++;
        }else{
            mp[A[i]]--;
            if(mp[A[i]]==0){
                mp.erase(A[i]);
                M.erase(A[i]);
            }
        }
    }
    rep(i,q)cout<<ans[i]<<"\n";
}
0