結果

問題 No.2220 Range Insert & Point Mex
ユーザー otoshigootoshigo
提出日時 2023-02-18 14:47:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 352 ms / 2,000 ms
コード長 2,866 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 226,700 KB
実行使用メモリ 13,644 KB
最終ジャッジ日時 2023-09-28 18:20:39
合計ジャッジ時間 8,379 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 91 ms
9,716 KB
testcase_14 AC 92 ms
10,172 KB
testcase_15 AC 92 ms
9,940 KB
testcase_16 AC 92 ms
10,512 KB
testcase_17 AC 93 ms
10,012 KB
testcase_18 AC 92 ms
11,076 KB
testcase_19 AC 92 ms
10,452 KB
testcase_20 AC 352 ms
12,184 KB
testcase_21 AC 343 ms
12,916 KB
testcase_22 AC 345 ms
12,132 KB
testcase_23 AC 147 ms
12,724 KB
testcase_24 AC 120 ms
11,256 KB
testcase_25 AC 91 ms
8,324 KB
testcase_26 AC 126 ms
13,644 KB
testcase_27 AC 104 ms
10,808 KB
testcase_28 AC 21 ms
4,692 KB
testcase_29 AC 24 ms
4,700 KB
testcase_30 AC 24 ms
4,676 KB
testcase_31 AC 63 ms
10,200 KB
testcase_32 AC 66 ms
6,412 KB
testcase_33 AC 69 ms
6,472 KB
testcase_34 AC 159 ms
10,484 KB
testcase_35 AC 158 ms
10,512 KB
testcase_36 AC 176 ms
11,212 KB
testcase_37 AC 174 ms
11,016 KB
testcase_38 AC 119 ms
10,936 KB
権限があれば一括ダウンロードができます

ソースコード

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