結果
問題 | No.649 ここでちょっとQK! |
ユーザー |
![]() |
提出日時 | 2024-03-07 19:41:33 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 348 ms / 3,000 ms |
コード長 | 2,568 bytes |
コンパイル時間 | 2,052 ms |
コンパイル使用メモリ | 213,308 KB |
実行使用メモリ | 12,800 KB |
最終ジャッジ日時 | 2024-09-29 18:41:53 |
合計ジャッジ時間 | 8,063 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 32 |
ソースコード
#include<bits/stdc++.h> using namespace std; typedef long long ll; random_device seed_gen; mt19937 engine(seed_gen()); uniform_int_distribution<int> dist_treap(0, (1 << 30) - 1); struct treap { struct node { node *lch, *rch; int par; ll val; int cnt; node(ll v, int p): lch(nullptr), rch(nullptr), par(p), val(v), cnt(1) {} }; int calc_cnt(node* t){ if (t == nullptr) return 0; return t->cnt; } node* update(node* t){ t->cnt = calc_cnt(t->lch) + calc_cnt(t->rch) + 1; return t; } node* merge(node* a, node* b){ if (a == nullptr || b == nullptr){ if (a == nullptr) return b; return a; } if (a->par > b->par){ a->rch = merge(a->rch, b); return update(a); }else{ b->lch = merge(a, b->lch); return update(b); } } pair<node*, node*> split(node* t, int k){ if (t == nullptr) return pair(nullptr, nullptr); assert(0 <= k && k <= calc_cnt(t)); if (calc_cnt(t->lch) >= k){ pair<node*, node*> ret = split(t->lch, k); t->lch = ret.second; return pair(ret.first, update(t)); }else{ pair<node*, node*> ret = split(t->rch, k - calc_cnt(t->lch) - 1); t->rch = ret.first; return pair(update(t), ret.second); } } node* insert(node* t, int k, ll v){ return insert(t, k, v, dist_treap(engine)); } node* insert(node* t, int k, ll v, int p){ pair<node*, node*> tar = split(t, k); node* var = new node(v, p); return merge(merge(tar.first, var), tar.second); } node* erase(node* t, int k){ assert(0 <= k && k < calc_cnt(t)); pair<node*, node*> spl1 = split(t, k + 1); pair<node*, node*> spl2 = split(spl1.first, k); delete spl2.second; return merge(spl2.first, spl1.second); } // returns the number of elements less than v int lower_bound(node* t, ll v){ if (t == nullptr) return 0; if (v <= t->val){ return lower_bound(t->lch, v); }else{ return calc_cnt(t->lch) + 1 + lower_bound(t->rch, v); } } node* insert(node* t, ll v){ return insert(t, lower_bound(t, v), v); } // returns val ll get(node* t, int k){ assert(t != nullptr); if (k < calc_cnt(t->lch)){ return get(t->lch, k); }else if(calc_cnt(t->lch) == k){ return t->val; }else{ return get(t->rch, k - calc_cnt(t->lch) - 1); } } }; int main(){ treap T; treap::node* root = nullptr; int q,k; cin >> q >> k; while(q--){ int t; cin >> t; if (t == 1){ ll v; cin >> v; root = T.insert(root, v); }else{ if (T.calc_cnt(root) >= k){ cout << T.get(root, k-1) << '\n'; root = T.erase(root, k-1); }else{ cout << -1 << '\n'; } } } }