結果

問題 No.649 ここでちょっとQK!
ユーザー kazumakazuma
提出日時 2018-04-03 22:22:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 3,000 ms
コード長 2,165 bytes
コンパイル時間 2,939 ms
コンパイル使用メモリ 202,056 KB
実行使用メモリ 12,956 KB
最終ジャッジ日時 2023-09-08 15:33:48
合計ジャッジ時間 7,332 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 28 ms
4,380 KB
testcase_04 AC 90 ms
12,832 KB
testcase_05 AC 94 ms
12,908 KB
testcase_06 AC 92 ms
12,956 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 110 ms
7,316 KB
testcase_13 AC 108 ms
7,276 KB
testcase_14 AC 104 ms
7,208 KB
testcase_15 AC 113 ms
7,256 KB
testcase_16 AC 110 ms
7,684 KB
testcase_17 AC 125 ms
7,940 KB
testcase_18 AC 142 ms
8,256 KB
testcase_19 AC 159 ms
8,660 KB
testcase_20 AC 171 ms
9,104 KB
testcase_21 AC 203 ms
9,376 KB
testcase_22 AC 205 ms
9,756 KB
testcase_23 AC 232 ms
10,164 KB
testcase_24 AC 241 ms
10,572 KB
testcase_25 AC 257 ms
10,920 KB
testcase_26 AC 271 ms
11,352 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 103 ms
7,312 KB
testcase_31 AC 100 ms
7,540 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 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;
	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;
}
0