結果
問題 | No.649 ここでちょっとQK! |
ユーザー | drken1215 |
提出日時 | 2018-02-14 02:19:51 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 485 ms / 3,000 ms |
コード長 | 6,219 bytes |
コンパイル時間 | 985 ms |
コンパイル使用メモリ | 75,904 KB |
実行使用メモリ | 12,928 KB |
最終ジャッジ日時 | 2024-05-08 20:57:43 |
合計ジャッジ時間 | 10,498 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 296 ms
5,376 KB |
testcase_04 | AC | 218 ms
12,928 KB |
testcase_05 | AC | 219 ms
12,672 KB |
testcase_06 | AC | 222 ms
12,672 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 212 ms
7,552 KB |
testcase_13 | AC | 204 ms
7,552 KB |
testcase_14 | AC | 204 ms
7,552 KB |
testcase_15 | AC | 219 ms
7,552 KB |
testcase_16 | AC | 208 ms
7,680 KB |
testcase_17 | AC | 241 ms
8,064 KB |
testcase_18 | AC | 263 ms
8,576 KB |
testcase_19 | AC | 288 ms
8,960 KB |
testcase_20 | AC | 318 ms
9,472 KB |
testcase_21 | AC | 345 ms
9,856 KB |
testcase_22 | AC | 371 ms
10,112 KB |
testcase_23 | AC | 399 ms
10,624 KB |
testcase_24 | AC | 434 ms
10,880 KB |
testcase_25 | AC | 462 ms
11,520 KB |
testcase_26 | AC | 485 ms
11,904 KB |
testcase_27 | AC | 3 ms
5,376 KB |
testcase_28 | AC | 3 ms
5,376 KB |
testcase_29 | AC | 3 ms
5,376 KB |
testcase_30 | AC | 177 ms
7,680 KB |
testcase_31 | AC | 176 ms
7,808 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 2 ms
5,376 KB |
testcase_35 | AC | 2 ms
5,376 KB |
ソースコード
#include <iostream> using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v.begin(), v.end()) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } typedef long long VAL; const VAL SUM_UNITY = 0; struct RBST { unsigned int randInt() { static unsigned int tx = 123456789, ty=362436069, tz=521288629, tw=88675123; unsigned int tt = (tx^(tx<<11)); tx = ty; ty = tz; tz = tw; return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) ); } struct NODE { NODE *left, *right; VAL val; // the value of the node int size; // the size of the subtree VAL sum; // the value-sum of the subtree NODE() : val(SUM_UNITY), size(1), sum(SUM_UNITY) { left = right = NULL; } NODE(VAL v) : val(v), size(1), sum(v) { left = right = NULL; } /* additional update */ inline void update() { } /* additional lazy-propagation */ inline void push() { /* ex: reverse */ /* if (this->rev) { swap(this->left, this->right); if (this->left) this->left->rev ^= true; if (this->right) this->right->rev ^= true; this->rev = false; } */ } }; /////////////////////// // root /////////////////////// NODE* root; RBST() : root(NULL) { } RBST(NODE* node) : root(node) { } /////////////////////// // basic operations /////////////////////// /* size */ inline int size(NODE *node) { return !node ? 0 : node->size; } inline int size() { return this->size(this->root); } /* sum */ inline VAL sum(NODE *node) { return !node ? SUM_UNITY : node->sum; } inline VAL sum() { return this->sum(this->root); } /* update, push */ inline NODE* update(NODE *node) { node->size = size(node->left) + size(node->right) + 1; node->sum = sum(node->left) + sum(node->right) + node->val; node->update(); return node; } inline void push(NODE *node) { if (!node) return; node->push(); } /* lower_bound */ inline int lowerBound(NODE *node, VAL val) { push(node); if (!node) return 0; if (val <= node->val) return lowerBound(node->left, val); else return size(node->left) + lowerBound(node->right, val) + 1; } inline int lowerBound(VAL val) { return this->lowerBound(this->root, val); } /* upper_bound */ inline int upperBound(NODE *node, VAL val) { push(node); if (!node) return 0; if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1; else return upperBound(node->left, val); } inline int upperBound(VAL val) { return this->upperBound(this->root, val); } /* count */ inline int count(VAL val) { return upperBound(val) - lowerBound(val); } /* get --- k: 0-index */ inline VAL get(NODE *node, int k) { push(node); if (!node) return -1; if (k == size(node->left)) return node->val; if (k < size(node->left)) return get(node->left, k); else return get(node->right, k - size(node->left) - 1); } inline VAL get(int k) { return get(this->root, k); } /////////////////////// // merge-split /////////////////////// NODE* merge(NODE *left, NODE *right) { push(left); push(right); if (!left || !right) { if (left) return left; else return right; } if (randInt() % (left->size + right->size) < left->size) { left->right = merge(left->right, right); return update(left); } else { right->left = merge(left, right->left); return update(right); } } void merge(RBST add) { this->root = this->merge(this->root, add.root); } pair<NODE*, NODE*> split(NODE* node, int k) { // [0, k), [k, n) push(node); if (!node) return make_pair(node, node); if (k <= size(node->left)) { pair<NODE*, NODE*> sub = split(node->left, k); node->left = sub.second; return make_pair(sub.first, update(node)); } else { pair<NODE*, NODE*> sub = split(node->right, k - size(node->left) - 1); node->right = sub.first; return make_pair(update(node), sub.second); } } RBST split(int k) { pair<NODE*, NODE*> sub = split(this->root, k); this->root = sub.first; return RBST(sub.second); } /////////////////////// // insert-erase /////////////////////// void insert(const VAL val) { pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val)); this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second); } void erase(const VAL val) { if (!this->count(val)) return; pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val)); this->root = this->merge(sub.first, this->split(sub.second, 1).second); } /////////////////////// // debug /////////////////////// void print(NODE *node) { if (!node) return; push(node); print(node->left); cout << node->val << " "; print(node->right); } void print() { cout << "{"; print(this->root); cout << "}" << endl; } }; int main() { RBST S; int Q, K; cin >> Q >> K; for (int i = 0; i < Q; ++i) { int T; cin >> T; if (T == 1) { long long val; cin >> val; S.insert(val); } else { if (S.size() < K) { cout << -1 << endl; continue; } long long res = S.get(K-1); cout << res << endl; S.erase(res); } //S.print(); } }