結果

問題 No.649 ここでちょっとQK!
ユーザー kazumakazuma
提出日時 2018-04-03 22:42:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 504 ms / 3,000 ms
コード長 3,818 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 203,796 KB
実行使用メモリ 175,492 KB
最終ジャッジ日時 2023-09-08 15:34:12
合計ジャッジ時間 9,532 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 28 ms
4,376 KB
testcase_04 AC 156 ms
23,944 KB
testcase_05 AC 159 ms
23,944 KB
testcase_06 AC 122 ms
32,124 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 190 ms
85,456 KB
testcase_13 AC 186 ms
85,504 KB
testcase_14 AC 188 ms
87,468 KB
testcase_15 AC 195 ms
87,472 KB
testcase_16 AC 186 ms
79,320 KB
testcase_17 AC 220 ms
91,652 KB
testcase_18 AC 249 ms
99,724 KB
testcase_19 AC 273 ms
110,088 KB
testcase_20 AC 308 ms
118,056 KB
testcase_21 AC 336 ms
127,696 KB
testcase_22 AC 370 ms
136,308 KB
testcase_23 AC 403 ms
146,736 KB
testcase_24 AC 425 ms
155,872 KB
testcase_25 AC 471 ms
165,244 KB
testcase_26 AC 504 ms
175,492 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 180 ms
85,552 KB
testcase_31 AC 183 ms
81,328 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

using T = ll;
const T id = LLONG_MAX;
T op(T l, T r) {
	return min(l, r);
}

enum COL { BLACK, RED };

struct node;
node *update(node *t);
struct node {
	T val, all;
	node *ch[2];
	COL color;
	int level;
	int size;
	node() {}
	void init(T v) {
		val = v;
		all = v;
		color = BLACK;
		level = 0;
		size = 1;
		ch[0] = ch[1] = nullptr;
	}
	void init(node *l, node *r, COL c) {
		val = id;
		color = c;
		ch[0] = l;
		ch[1] = r;
		update(this);
	}
};

const int pmax = 1e7;
node pool[pmax];
int it = 0;

int count(node *t) { return !t ? 0 : t->size; }

T que(node *t) { return !t ? id : t->all; }

int ranks(node *t) { return !t ? 0 : t->level; }

node *update(node *t) {
	t->size = count(t->ch[0]) + count(t->ch[1]);
	t->all = op(que(t->ch[0]), op(t->val, que(t->ch[1])));
	t->level = ranks(t->ch[0]) + (t->ch[0]->color == BLACK);
	return t;
}

node *new_leaf(T v) {
	assert(it < pmax);
	pool[it].init(v);
	return &pool[it++];
}

node *new_node(node *l, node *r, COL c) {
	assert(it < pmax);
	pool[it].init(l, r, c);
	return &pool[it++];
}

node *rotate(node *t, int b) {
	node *s = t->ch[1 - b];
	t->ch[1 - b] = s->ch[b];
	s->ch[b] = t;
	update(t); update(s);
	return s;
}

node *submerge(node *l, node *r) {
	if (l->level < r->level) {
		node *c = submerge(l, r->ch[0]);
		r->ch[0] = c;
		if (r->color == BLACK && c->color == RED && c->ch[0] && c->ch[0]->color == RED) {
			if (r->ch[1]->color == BLACK) {
				r->color = RED;
				c->color = BLACK;
				return rotate(r, 1);
			}
			else {
				c->color = BLACK;
				r->ch[1]->color = BLACK;
				r->color = RED;
				return update(r);
			}
		}
		else {
			return update(r);
		}
	}
	else if (l->level > r->level) {
		node *c = submerge(l->ch[1], r);
		l->ch[1] = c;
		if (l->color == BLACK && c->color == RED && c->ch[1] && c->ch[1]->color == RED) {
			if (l->ch[0]->color == BLACK) {
				l->color = RED;
				c->color = BLACK;
				return rotate(l, 0);
			}
			else {
				l->ch[0]->color = BLACK;
				c->color = BLACK;
				l->color = RED;
				return update(l);
			}
		}
		else {
			return update(l);
		}
	}
	else {
		return new_node(l, r, RED);
	}
}

node *merge(node *l, node *r) {
	if (!l || !r) return !l ? r : l;
	node *c = submerge(l, r);
	c->color = BLACK;
	return c;
}

pair<node*, node*> split(node *t, int k) {
	if (!t) return make_pair(nullptr, nullptr);
	if (k == 0) return make_pair(nullptr, t);
	if (k >= count(t)) return make_pair(t, nullptr);
	int c = count(t->ch[0]);
	if (k < c) {
		pair<node*, node*> p = split(t->ch[0], k);
		return make_pair(p.first, merge(p.second, t->ch[1]));
	}
	else if (k > c) {
		pair<node*, node*> p = split(t->ch[1], k - c);
		return make_pair(merge(t->ch[0], p.first), p.second);
	}
	else {
		return make_pair(t->ch[0], t->ch[1]);
	}
}

node *insert(node *t, int k, T v) {
	pair<node*, node*> s = split(t, k);
	return merge(merge(s.first, new_leaf(v)), s.second);
}

node *erase(node *t, int k) {
	pair<node*, node*> s1 = split(t, k), s2 = split(s1.second, 1);
	return merge(s1.first, s2.second);
}

node *find(node *t, int k) {
	if (!t) return t;
	int c = count(t->ch[0]);
	return c == 0 ? t : k < c ? find(t->ch[0], k) : find(t->ch[1], k - c);
}

int cnt(node *t, T v) {
	if (!t) return 0;
	if (!t->ch[1]) return t->val < v;
	if (v > t->ch[1]->all) return count(t->ch[0]) + cnt(t->ch[1], v);
	if (v == t->ch[1]->all) return count(t->ch[0]);
	return cnt(t->ch[0], 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