結果

問題 No.649 ここでちょっとQK!
ユーザー ミドリムシミドリムシ
提出日時 2021-11-12 23:03:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 637 ms / 3,000 ms
コード長 3,995 bytes
コンパイル時間 1,886 ms
コンパイル使用メモリ 166,496 KB
実行使用メモリ 247,708 KB
最終ジャッジ日時 2023-08-17 03:32:45
合計ジャッジ時間 11,312 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 270 ms
4,380 KB
testcase_04 AC 101 ms
15,936 KB
testcase_05 AC 98 ms
15,828 KB
testcase_06 AC 73 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 299 ms
128,428 KB
testcase_13 AC 302 ms
128,472 KB
testcase_14 AC 281 ms
118,864 KB
testcase_15 AC 300 ms
128,596 KB
testcase_16 AC 287 ms
128,556 KB
testcase_17 AC 324 ms
140,500 KB
testcase_18 AC 355 ms
152,524 KB
testcase_19 AC 395 ms
164,260 KB
testcase_20 AC 423 ms
176,496 KB
testcase_21 AC 458 ms
188,312 KB
testcase_22 AC 506 ms
200,284 KB
testcase_23 AC 526 ms
212,508 KB
testcase_24 AC 559 ms
224,196 KB
testcase_25 AC 597 ms
236,052 KB
testcase_26 AC 637 ms
247,708 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 73 ms
8,200 KB
testcase_31 AC 68 ms
8,040 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 998244353;
#define all(x) begin(x), end(x)
#define bitcount(n) __builtin_popcountll((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzll(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class itr> void array_output(itr start, itr goal){ for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); cout << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(b) return gcd(b, a % b); else return a; }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b < a){ a = b; return 1; } return 0; }
inline int has(lint i, int j){ return (i >> j) & 1; }
int dy[4] = {1, 0, -1, 0}; int dx[4] = {0, 1, 0, -1};
bool is_inside(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }

struct io_init {
    io_init() {
        cin.tie(nullptr); cout.tie(nullptr);
        std::ios::sync_with_stdio(false);
    }
} io_init;

template<class T, int BIT_COUNT = -1>
struct BinaryTrie{
    struct Node {
        Node *child[2];
        int under_cnt;
        
        Node() : child{nullptr, nullptr}, under_cnt(0) {}
    };
    
    int B;
    Node *root;
    
    BinaryTrie() : root(new Node()) {
        if(BIT_COUNT == -1){
            B = numeric_limits<T>::digits + int(numeric_limits<T>::is_signed);
        }else{
            B = BIT_COUNT;
        }
    }
    
    void insert(T x){
        Node *at = root;
        repr(i, B){
            (at -> under_cnt)++;
            
            bool d = ((x >> i) & 1);
            if(!(at -> child[d])) (at -> child[d]) = new Node();
            at = at -> child[d];
        }
        (at -> under_cnt)++;
    }
    
    int count(T x){
        Node *at = root;
        repr(i, B){
            bool d = ((x >> i) & 1);
            at = at -> child[d];
            if(!at) return 0;
        }
        return at -> under_cnt;
    }
    
    bool erase(T x){
        Node *at = root;
        repr(i, B){
            bool d = ((x >> i) & 1);
            at = at -> child[d];
            if(!at) return false;
        }
        
        at = root;
        repr(i, B){
            (at -> under_cnt)--;
            
            bool d = ((x >> i) & 1);
            at = at -> child[d];
        }
        (at -> under_cnt)--;
        return true;
    }
    
    int size(){
        return root -> under_cnt;
    }
    
    T kth_element(int k){
        assert(0 <= k && k < size());
        k++;
        
        Node *at = root;
        T ans = 0, one = 1;
        repr(i, B){
            int l_cnt = (at -> child[0] ? at -> child[0] -> under_cnt : 0);
            if(k <= l_cnt){
                at = at -> child[0];
            }else{
                at = at -> child[1];
                k -= l_cnt;
                ans += (one << i);
            }
        }
        return ans;
    }
};

int main(){
    int q, k;
    cin >> q >> k;
    k--;
    BinaryTrie<lint> tree;
    rep(_, q){
        int t;
        cin >> t;
        if(t == 1){
            lint x;
            cin >> x;
            tree.insert(x);
        }else{
            if(tree.size() < k + 1){
                cout << -1 << endl;
            }else{
                lint ans = tree.kth_element(k);
                cout << ans << endl;
                tree.erase(ans);
            }
        }
    }
}
0