結果

問題 No.649 ここでちょっとQK!
ユーザー kazumakazuma
提出日時 2018-04-03 22:00:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 3,000 ms
コード長 2,174 bytes
コンパイル時間 3,515 ms
コンパイル使用メモリ 200,904 KB
実行使用メモリ 13,072 KB
最終ジャッジ日時 2023-09-08 15:33:20
合計ジャッジ時間 8,068 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 29 ms
4,380 KB
testcase_04 AC 100 ms
12,876 KB
testcase_05 AC 103 ms
12,988 KB
testcase_06 AC 87 ms
13,072 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 131 ms
7,488 KB
testcase_13 AC 131 ms
7,252 KB
testcase_14 AC 128 ms
7,228 KB
testcase_15 AC 140 ms
7,316 KB
testcase_16 AC 131 ms
7,660 KB
testcase_17 AC 152 ms
7,972 KB
testcase_18 AC 168 ms
8,356 KB
testcase_19 AC 186 ms
8,652 KB
testcase_20 AC 209 ms
9,048 KB
testcase_21 AC 229 ms
9,592 KB
testcase_22 AC 243 ms
9,796 KB
testcase_23 AC 263 ms
10,216 KB
testcase_24 AC 284 ms
10,692 KB
testcase_25 AC 306 ms
10,968 KB
testcase_26 AC 323 ms
11,288 KB
testcase_27 AC 3 ms
4,384 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 125 ms
7,264 KB
testcase_31 AC 126 ms
7,664 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,384 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
	int size;
	node(T v, node* l = nullptr, node* r = nullptr) : val(v), lch(l), rch(r), 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 (xor128() % (l->size + r->size) < (unsigned)l->size) {
		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));
	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 != nullptr) delete s2.first;
	return merge(s1.first, s2.second);
}

node *find(node *t, int k) {
	if (t == nullptr) 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 == nullptr) 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;
}
0