結果

問題 No.649 ここでちょっとQK!
ユーザー kazumakazuma
提出日時 2018-04-03 22:15:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 3,000 ms
コード長 4,447 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 201,752 KB
実行使用メモリ 12,976 KB
最終ジャッジ日時 2023-09-08 15:33:31
合計ジャッジ時間 6,126 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 28 ms
4,376 KB
testcase_04 AC 135 ms
12,860 KB
testcase_05 AC 144 ms
12,976 KB
testcase_06 AC 114 ms
12,888 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,388 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 73 ms
7,780 KB
testcase_13 AC 69 ms
7,736 KB
testcase_14 AC 69 ms
7,888 KB
testcase_15 AC 74 ms
7,704 KB
testcase_16 AC 71 ms
7,708 KB
testcase_17 AC 82 ms
8,108 KB
testcase_18 AC 90 ms
8,564 KB
testcase_19 AC 100 ms
8,992 KB
testcase_20 AC 110 ms
9,460 KB
testcase_21 AC 122 ms
9,840 KB
testcase_22 AC 133 ms
10,312 KB
testcase_23 AC 142 ms
10,852 KB
testcase_24 AC 153 ms
11,060 KB
testcase_25 AC 162 ms
11,556 KB
testcase_26 AC 176 ms
11,920 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 63 ms
7,868 KB
testcase_31 AC 61 ms
7,732 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
class red_black_tree {

	using COL = bool;
	const static COL RED = false, BLACK = true;

	struct node {
		COL color;
		int size;
		T val;
		node *p, *ch[2];
		node() : color(BLACK), size(0), val(), p(nullptr), ch{ nullptr, nullptr } {}
		node(COL c, T v, node *par, node *l, node *r) : color(c), size(1), val(v), p(par), ch{ l, r } {}
	} *NIL, *root;

	node* new_node(T val) {
		return new node(RED, val, NIL, NIL, NIL);
	}
	void update(node *x) {
		x->size = x->ch[0]->size + 1 + x->ch[1]->size;
	}
	void update_up(node *x) {
		while (x != NIL) update(x), x = x->p;
	}
	void rotate(node *x, int b) {
		node *y = x->ch[1 - b];
		x->ch[1 - b] = y->ch[b];
		if (y->ch[b] != NIL) {
			y->ch[b]->p = x;
		}
		y->p = x->p;
		if (x->p == NIL) {
			root = y;
		}
		else {
			x->p->ch[x != x->p->ch[0]] = y;
		}
		y->ch[b] = x;
		x->p = y;
		update(x);
		update(y);
	}
	void insert_fix(node *x) {
		while (x->p->color == RED) {
			int b = (x->p != x->p->p->ch[0]);
			node *y = x->p->p->ch[1 - b];
			if (y->color == RED) {
				x->p->color = BLACK;
				y->color = BLACK;
				x->p->p->color = RED;
				x = x->p->p;
				continue;
			}
			if (x == x->p->ch[1 - b]) {
				x = x->p;
				rotate(x, b);
			}
			x->p->color = BLACK;
			x->p->p->color = RED;
			rotate(x->p->p, 1 - b);
		}
		root->color = BLACK;
	}
	void transplant(node *u, node *v) {
		if (u->p == NIL) {
			root = v;
		}
		else {
			u->p->ch[u != u->p->ch[0]] = v;
		}
		v->p = u->p;
	}
	void erase_fix(node *x) {
		while (x != root && x->color == BLACK) {
			int b = (x != x->p->ch[0]);
			node *w = x->p->ch[1 - b];
			if (w->color == RED) {
				w->color = BLACK;
				x->p->color = RED;
				rotate(x->p, b);
				w = x->p->ch[1 - b];
			}
			if (w->ch[b]->color == BLACK && w->ch[1 - b]->color == BLACK) {
				w->color = RED;
				x = x->p;
				continue;
			}
			if (w->ch[1 - b]->color == BLACK) {
				w->ch[b]->color = BLACK;
				w->color = RED;
				rotate(w, 1 - b);
				w = x->p->ch[1 - b];
			}
			w->color = x->p->color;
			x->p->color = BLACK;
			w->ch[1 - b]->color = BLACK;
			rotate(x->p, b);
			x = root;
		}
		x->color = BLACK;
	}
	node* find_first(node *x) {
		if (x == NIL) return NIL;
		while (x->ch[0] != NIL) x = x->ch[0];
		return x;
	}
	node* find(node *t, int k) {
		if (k < 0 || t->size <= k) return NIL;
		node *x = t;
		while (x->ch[0]->size != k) {
			if (k < x->ch[0]->size) {
				x = x->ch[0];
			}
			else {
				k -= x->ch[0]->size + 1;
				x = x->ch[1];
			}
		}
		return x;
	}

public:

	red_black_tree() : NIL(new node()), root(NIL) {}
	int size() {
		return root->size;
	}
	T find(int k) {
		return find(root, k)->val;
	}
	void update(int k, T val) {
		node *t = find(root, k);
		t->val = val;
	}
	int count_lower(T val) {
		node *t = root;
		int res = 0;
		while (t != NIL) {
			if (t->val == val) return res + t->ch[0]->size;
			if (t->val < val) {
				res += t->ch[0]->size + 1;
				t = t->ch[1];
			}
			else {
				t = t->ch[0];
			}
		}
		return res;
	}
	void insert(int k, T val) {
		node *y = NIL, *v = new_node(val);
		if (root == NIL) {
			root = v;
		}
		else if (k == 0) {
			y = find_first(root);
			y->ch[0] = v;
		}
		else {
			y = find(root, k - 1);
			if (y->ch[1] == NIL) {
				y->ch[1] = v;
			}
			else {
				y = find_first(y->ch[1]);
				y->ch[0] = v;
			}
		}
		v->p = y;
		update_up(y);
		insert_fix(v);
	}
	void erase(int k) {
		node *x = find(root, k);
		erase(x);
	}
	void erase(node *x) {
		if (x == NIL) return;
		node *y = x, *z;
		COL c = y->color;
		if (x->ch[0] == NIL) {
			z = x->ch[1];
			transplant(x, x->ch[1]);
		}
		else if (x->ch[1] == NIL) {
			z = x->ch[0];
			transplant(x, x->ch[0]);
		}
		else {
			y = find_first(x->ch[1]);
			c = y->color;
			z = y->ch[1];
			if (y->p == x) {
				z->p = y;
			}
			else {
				transplant(y, y->ch[1]);
				y->ch[1] = x->ch[1];
				y->ch[1]->p = y;
			}
			transplant(x, y);
			y->ch[0] = x->ch[0];
			y->ch[0]->p = y;
			y->color = x->color;
			update(y);
		}
		update_up(z->p);
		if (c == BLACK) erase_fix(z);
	}

};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int Q, K;
	cin >> Q >> K;
	red_black_tree<ll> tree;
	while (Q--) {
		int t;
		cin >> t;
		if (t == 1) {
			ll v;
			cin >> v;
			tree.insert(tree.count_lower(v), v);
		}
		else if (tree.size() >= K) {
			printf("%lld\n", tree.find(K - 1));
			tree.erase(K - 1);
		}
		else {
			printf("%d\n", -1);
		}
	}
	return 0;
}
0