結果
問題 |
No.649 ここでちょっとQK!
|
ユーザー |
|
提出日時 | 2019-03-07 15:08:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 352 ms / 3,000 ms |
コード長 | 4,246 bytes |
コンパイル時間 | 757 ms |
コンパイル使用メモリ | 79,000 KB |
実行使用メモリ | 14,464 KB |
最終ジャッジ日時 | 2024-06-23 14:50:13 |
合計ジャッジ時間 | 6,643 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 32 |
ソースコード
#include <iostream> #include <vector> using namespace std; using i64 = int64_t; using u32 = uint32_t; u32 uy = u32(time(NULL)); u32 xorshift32() { uy ^= uy << 14; uy ^= uy >> 13; uy ^= uy << 15; return uy; } template <typename T> bool __attribute__((noinline)) is_null(T* obj) { return obj == nullptr; } // RBST (Randomised Binary Search Tree) template <class T> struct RBST { struct Node { // {{{ Node constexpr static T nullval = -1; // valueAt() で不正な場所が指定されたときに返す値. T val; int treeSize; Node *lo, *hi; explicit Node(T x) : val(x), treeSize(1), lo(nullptr), hi(nullptr) {} int size() { if(is_null(this)) { return 0; } return treeSize; } Node* update() { treeSize = lo->size() + hi->size() + 1; return this; } static Node* merge(Node *l, Node *r) { if(is_null(l)) { return r; } if(is_null(r)) { return l; } size_t lSize = l->size(), rSize = r->size(); if(xorshift32() % (lSize + rSize) < lSize) { l->hi = merge(l->hi, r); return l->update(); } else { r->lo = merge(l, r->lo); return r->update(); } } pair<Node*, Node*> split(int k) { // [0, k), [k, n) if(is_null(this)) { return make_pair(nullptr, nullptr); } if(k <= lo->size()) { auto s = lo->split(k); lo = s.second; return make_pair(s.first, update()); } else { auto s = hi->split(k - lo->size() - 1); hi = s.first; return make_pair(update(), s.second); } } int indexOf(T x) { if(is_null(this)) { return -1; } if(x == this->val) { return lo->size(); } if(x < this->val) { return lo->indexOf(x); } else { return lo->size() + 1 + hi->indexOf(x); } } T valueAt(int pos) { if(is_null(this)) { return nullval; } int loSize = lo->size(); if(loSize == pos) { return val; } if(loSize > pos) { return lo->valueAt(pos); } else { return hi->valueAt(pos - lo->size() - 1); } } int lowerBound(T x) { if(is_null(this)) { return 0; } if(x <= this->val) { return lo->lowerBound(x); } return hi->lowerBound(x) + lo->size() + 1; } int upperBound(T x) { if(is_null(this)) { return 0; } if(x >= this->val) { return lo->size() + 1 + hi->upperBound(x); } return lo->upperBound(x); } int count(T x) { return upperBound(x) - lowerBound(x); } Node* insert(int pos, T x) { Node *p = new Node(x); auto s = split(pos); return merge(merge(s.first, p), s.second); } Node* insert(T x) { return insert(lowerBound(x), x); } Node* deleteAt(int pos) { auto s = split(pos); auto t = s.second->split(1); t.first = nullptr; return merge(s.first, t.second); } void dump(int x) { if(is_null(this)) { return; } fprintf(stderr, "%c", "([{"[x%3]); lo->dump(x+1); fprintf(stderr, "%ld", this->val); hi->dump(x+1); fprintf(stderr, "%c", ")]}"[x%3]); } }; // }}} Node *root; RBST() { root = nullptr; } int size() { return root->size(); } int indexOf(T val) { return root->indexOf(val); } T valueAt(int pos) { return root->valueAt(pos); } int lowerBound(T val) { return root->lowerBound(val); } int upperBound(T val) { return root->upperBound(val); } int count(T val) { return root->count(val); } void insert(T val) { root = root->insert(val); } void deleteAt(int pos) { root = root->deleteAt(pos); } void deleteValue(T val) { deleteAt(indexOf(val)); } void dump() { root->dump(0); fprintf(stderr, "\n"); } }; int main(void) { int Q, K; scanf("%d%d", &Q, &K); --K; RBST<i64> rbst; vector<i64> v(Q); int cmd; i64 vi; int size = 0; for(int i=0; i<Q; ++i) { scanf("%d", &cmd); switch(cmd) { case 1: scanf("%ld", &vi); rbst.insert(vi); ++size; break; case 2: if(K < size) { printf("%ld\n", rbst.valueAt(K)); rbst.deleteAt(K); --size; } else { puts("-1"); } break; } } return 0; }