結果
問題 | No.649 ここでちょっとQK! |
ユーザー | みずくらげ |
提出日時 | 2019-07-23 11:00:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,643 bytes |
コンパイル時間 | 1,674 ms |
コンパイル使用メモリ | 128,568 KB |
実行使用メモリ | 8,320 KB |
最終ジャッジ日時 | 2024-06-25 02:13:37 |
合計ジャッジ時間 | 7,660 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | AC | 295 ms
6,944 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | AC | 2 ms
6,940 KB |
ソースコード
#include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <set> #include <map> #include <queue> #include <iomanip> #include <cassert> #include <random> using namespace std; typedef long long ll; const ll mod = 1000000007; #define int long long struct node_t{ int val; node_t* lch; node_t* rch; int cnt; int sum; node_t() : val(0), cnt(1), sum(0) {lch = rch = NULL;} node_t(int v) : val(v), cnt(1), sum(v) {lch = rch = NULL;} }; random_device rd; mt19937 gen(rd()); uniform_int_distribution<int> dis(0, 10000000); struct Tree { node_t* root; Tree() : root(NULL) {} Tree(node_t* t) : root(t) {} int cnt(node_t* t) {return !t ?0 :t->cnt;} int cnt() {return this->cnt(this->root);} int sum(node_t* t) {return !t ?0 :t->sum;} int sum() {return this->sum(this->root);} node_t* update(node_t* t) { t->cnt = cnt(t->lch) + cnt(t->rch) + 1; t->sum = sum(t->lch) + sum(t->rch) + t->val; return t; } int lowerBound(node_t* t, int val) { if (!t) return 0; if (val <= t->val) return lowerBound(t->lch, val); else return cnt(t->lch) + lowerBound(t->rch, val) + 1; } int lowerBound(int val) {return this->lowerBound(this->root, val);} int upperBound(node_t* t, int val) { return lowerBound(t, val+1); } int upperBound(int val) {return this->lowerBound(this->root, val+1);} int get(node_t* t, int k) { if (!t) return -1; if (k == cnt(t->lch)) return t->val; if (k < cnt(t->lch)) return get(t->lch, k); else return get(t->rch, k - cnt(t->lch) - 1); } int get(int k) {return get(this->root, k);} node_t* merge(node_t* l, node_t* r) { if (!l || !r) return !l ?r :l; if (dis(gen) % (l->cnt + r->cnt) < l->cnt) { l->rch = merge(l->rch, r); return update(l); } else { r->lch = merge(r->lch, l); return update(r); } } void merge(Tree add) {this->root = this->merge(this->root, add.root);} pair<node_t*, node_t*> split(node_t* t, int k) { // [0, k), [k, n) if (!t) return make_pair(t, t); if (k <= cnt(t->lch)) { pair<node_t*, node_t*> s = split(t->lch, k); t->lch = s.second; return make_pair(s.first, update(t)); } else { pair<node_t*, node_t*> s = split(t->rch, k - cnt(t->lch) - 1); t->rch = s.first; return make_pair(update(t), s.second); } } Tree split(int k) { pair<node_t*, node_t*> s = split(this->root, k); this->root = s.first; return Tree(s.second); } void insert(int val) { pair<node_t*, node_t*> s = this->split(this->root, this->lowerBound(val)); this->root = this->merge(this->merge(s.first, new node_t(val)), s.second); } void erase(int val) { if (!(this->upperBound(val) - this->lowerBound(val))) return; pair<node_t*, node_t*> s = split(this->root, this->lowerBound(val)); this->root = this->merge(s.first, this->split(s.second, 1).second); } }; signed main() { int q, k; cin >> q >> k; Tree tree; int t; for (int i = 0; i < q; i++) { cin >> t; if (t == 1) { tree.insert(k); } else { if (tree.cnt() < k) { cout << -1 << endl; continue; } int val = tree.get(k-1); cout << val << endl; tree.erase(val); } } return 0; }