結果

問題 No.649 ここでちょっとQK!
ユーザー hashiryohashiryo
提出日時 2019-06-26 23:59:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 777 ms / 3,000 ms
コード長 2,471 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 171,584 KB
実行使用メモリ 370,568 KB
最終ジャッジ日時 2023-09-08 14:16:08
合計ジャッジ時間 13,755 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 277 ms
4,380 KB
testcase_04 AC 138 ms
22,408 KB
testcase_05 AC 136 ms
22,336 KB
testcase_06 AC 106 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 380 ms
195,032 KB
testcase_13 AC 369 ms
195,164 KB
testcase_14 AC 339 ms
180,724 KB
testcase_15 AC 370 ms
192,260 KB
testcase_16 AC 354 ms
191,236 KB
testcase_17 AC 405 ms
209,440 KB
testcase_18 AC 429 ms
227,428 KB
testcase_19 AC 484 ms
244,924 KB
testcase_20 AC 534 ms
263,272 KB
testcase_21 AC 571 ms
281,236 KB
testcase_22 AC 599 ms
299,088 KB
testcase_23 AC 654 ms
317,128 KB
testcase_24 AC 697 ms
335,076 KB
testcase_25 AC 737 ms
352,768 KB
testcase_26 AC 777 ms
370,568 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,384 KB
testcase_30 AC 96 ms
13,744 KB
testcase_31 AC 87 ms
10,528 KB
testcase_32 AC 2 ms
4,388 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
const ll INF = LLONG_MAX/2;
const ll MOD = 1e9+7;
const double EPS=1e-12;

template<typename KEYTYPE = unsigned long long, int B = 64>
class BinaryTrie {
    struct node {
        int cnt;
        KEYTYPE lazy;
        node *ch[2];
        node() : cnt(0), lazy(0), ch{ nullptr, nullptr } {}
    };
    node* insert_node(node* t, KEYTYPE key, int b = B - 1) {
        if (!t) t = new node;
        t->cnt += 1;
        if (b < 0) return t;
        push(t, b);
        bool f = (key >> (KEYTYPE)b) & (KEYTYPE)1;
        t->ch[f] = insert_node(t->ch[f], key, b - 1);
        return t;
    }
    node* erase_node(node* t, KEYTYPE key, int b = B - 1) {
        assert(t);
        t->cnt -= 1;
        if (t->cnt == 0) return nullptr;
        if (b < 0) return t;
        push(t, b);
        bool f = (key >> (KEYTYPE)b) & (KEYTYPE)1;
        t->ch[f] = erase_node(t->ch[f], key, b - 1);
        return t;
    }
    //[
    void push(node* t, int b) {
        if ((t->lazy >> (KEYTYPE)b) & (KEYTYPE)1) swap(t->ch[0], t->ch[1]);
        if (t->ch[0]) t->ch[0]->lazy ^= t->lazy;
        if (t->ch[1]) t->ch[1]->lazy ^= t->lazy;
        t->lazy = 0;
    }
    KEYTYPE get(node* t, int k, int b = B - 1) {
        if (b < 0) return 0;
        push(t, b);
        int m = t->ch[0] ? t->ch[0]->cnt : 0;
        return k < m ? get(t->ch[0], k, b - 1) : get(t->ch[1], k - m, b - 1) | ((KEYTYPE)1 << (KEYTYPE)b);
    }
    //]
    node *root;
public:
    BinaryTrie() : root(nullptr) {}
    void insert(KEYTYPE key) {
        root = insert_node(root, key);
    }
    void erase(KEYTYPE key) {
        root = erase_node(root, key);
    }
    //[
    int size() const {
        return root ? root->cnt : 0;
    }
    KEYTYPE kth_element(int k) {
        assert(0 <= k && k < size());
        return get(root, k);
    }
    //]
};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll Q,K;cin>>Q>>K;K--;
  BinaryTrie<> S;
  for(ll q=0;q<Q;q++){
    ll op;cin>>op;
    if(op&1){
      ll v;cin>>v;
      S.insert(v);
    }else{
      ll ans=-1;
      if(S.size()>K){
        ans = S.kth_element(K);
        S.erase(ans);
      }
      cout<<ans<<endl;
    }
  }
  return 0;
}
0