結果
| 問題 |
No.649 ここでちょっとQK!
|
| コンテスト | |
| ユーザー |
kazuma
|
| 提出日時 | 2018-04-03 22:22:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 270 ms / 3,000 ms |
| コード長 | 2,165 bytes |
| コンパイル時間 | 2,053 ms |
| コンパイル使用メモリ | 196,220 KB |
| 最終ジャッジ日時 | 2025-01-05 09:54:02 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
unsigned xor128() {
static unsigned x = 123456789, y = 362436069, z = 521288629, w = 88675123;
unsigned t = x ^ (x << 11);
x = y; y = z; z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
using T = ll;
struct node {
T val;
node *lch, *rch;
unsigned int pri;
int size;
node(T v, unsigned int p, node *l = nullptr, node *r = nullptr) : val(v), lch(l), rch(r), pri(p), size(1) {}
};
int count(node *t) { return !t ? 0 : t->size; }
node *update(node *t) {
t->size = count(t->lch) + count(t->rch) + 1;
return t;
}
node *merge(node *l, node *r) {
if (!l || !r) return !l ? r : l;
if (l->pri > r->pri) {
l->rch = merge(l->rch, r);
return update(l);
}
r->lch = merge(l, r->lch);
return update(r);
}
pair<node*, node*> split(node *t, int k) {
if (!t) return make_pair(nullptr, nullptr);
if (k <= count(t->lch)) {
pair<node*, node*> s = split(t->lch, k);
t->lch = s.second;
return make_pair(s.first, update(t));
}
pair<node*, node*> s = split(t->rch, k - count(t->lch) - 1);
t->rch = s.first;
return make_pair(update(t), s.second);
}
node *insert(node *t, int k, T v) {
pair<node*, node*> s = split(t, k);
node *r = merge(s.first, new node(v, xor128()));
r = merge(r, s.second);
return r;
}
node *erase(node *t, int k) {
pair<node*, node*> s1 = split(t, k), s2 = split(s1.second, 1);
if (s2.first) delete s2.first;
return merge(s1.first, s2.second);
}
node *find(node *t, int k) {
if (!t) return t;
int c = count(t->lch);
return k < c ? find(t->lch, k) : k == c ? t : find(t->rch, k - (c + 1));
}
int cnt(node *t, T v) {
if (!t) return 0;
if (t->val < v) return count(t->lch) + 1 + cnt(t->rch, v);
if (t->val == v) return count(t->lch);
return cnt(t->lch, v);
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int Q, K;
cin >> Q >> K;
node *root = nullptr;
while (Q--) {
int t;
cin >> t;
if (t == 1) {
ll v;
cin >> v;
root = insert(root, cnt(root, v), v);
}
else if (count(root) >= K) {
printf("%lld\n", find(root, K - 1)->val);
root = erase(root, K - 1);
}
else {
printf("%d\n", -1);
}
}
return 0;
}
kazuma