Submission #1494403


Source Code Expand

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

int id = 0, INF = INT_MAX;
enum COL { BLACK, RED };

template <typename T>
struct node;
template <typename T>
node<T> *update(node<T> *t);

template <typename T>
struct node {
	T val, mini;
	node *ch[2];
	COL color;
	int level;
	int size;
	node() {}
	void init(T v) {
		val = v;
		mini = 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 = 5e6;
node<int> pool[pmax];
int it = 0;

template <typename T>
int minimum(node<T> *t) { return t == nullptr ? INF : t->mini; }
template <typename T>
int ranks(node<T> *t) { return t == nullptr ? 0 : t->level; }
template <typename T>
int count(node<T> *t) { return t == nullptr ? 0 : t->size; }
template <typename T>
node<T> *update(node<T> *t) {
	t->mini = min(minimum(t->ch[0]), minimum(t->ch[1]));
	t->level = ranks(t->ch[0]) + (t->ch[0]->color == BLACK);
	t->size = count(t->ch[0]) + count(t->ch[1]);
	return t;
}
template <typename T>
node<T> *new_leaf(T v) {
	if (it == pmax) exit(EXIT_SUCCESS);
	pool[it].init(v);
	return &pool[it++];
}
template <typename T>
node<T> *new_node(node<T> *l, node<T> *r, COL c) {
	if (it == pmax) exit(EXIT_SUCCESS);
	pool[it].init(l, r, c);
	return &pool[it++];
}
template <typename T>
node<T> *rotate(node<T> *t, int b) {
	node<T> *s = t->ch[1 - b];
	t->ch[1 - b] = s->ch[b];
	s->ch[b] = t;
	update(t); update(s);
	return s;
}
template <typename T>
node<T> *submerge(node<T> *l, node<T> *r) {
	if (l->level < r->level) {
		node<T> *c = submerge(l, r->ch[0]);
		r->ch[0] = c;
		if (r->color == BLACK && c->color == RED && c->ch[0] != nullptr && 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<T> *c = submerge(l->ch[1], r);
		l->ch[1] = c;
		if (l->color == BLACK && c->color == RED && c->ch[1] != nullptr && 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);
	}
}
template <typename T>
node<T> *merge(node<T> *l, node<T> *r) {
	if (l == nullptr || r == nullptr) return l == nullptr ? r : l;
	node<T> *c = submerge(l, r);
	c->color = BLACK;
	return c;
}
template <typename T>
pair<node<T>*, node<T>*> split(node<T> *t, int k) {
	if (t == nullptr) 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<T>*, node<T>*> p = split(t->ch[0], k);
		return make_pair(p.first, merge(p.second, t->ch[1]));
	}
	else if (k > c) {
		pair<node<T>*, node<T>*> 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]);
	}
}
template <typename T>
node<T> *insert(node<T> *t, int k, int v) {
	pair<node<T>*, node<T>*> s = split(t, k);
	return merge(merge(s.first, new_leaf(v)), s.second);
}
template <typename T>
node<T> *erase(node<T> *t, int k) {
	pair<node<T>*, node<T>*> s1 = split(t, k), s2 = split(s1.second, 1);
	return merge(s1.first, s2.second);
}
template <typename T>
T find(node<T> *t, int l, int r) {
	if (t == nullptr) return id;
	if (r < 0 || l >= count(t)) return id;
	if (l <= 0 && r >= t->size) return t->all;
	int c = count(t->ch[0]);
	return merge(t->ch[0] == nullptr ? id : find(t->ch[0], l, r), merge(l <= c && c < r ? t->val : id, t->ch[1] == nullptr ? id : find(t->ch[1], l - (c + 1), r - (c + 1))));
}
template <typename T>
node<T> *find(node<T> *t, int k) {
	if (t == nullptr) 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);
}
template <typename T>
int cnt(node<T> *t, T v) {
	if (t == nullptr) return 0;
	if (t->ch[1] == nullptr) return t->val < v;
	if (v > t->ch[1]->mini) return count(t->ch[0]) + cnt(t->ch[1], v);
	if (v == t->ch[1]->mini) return count(t->ch[0]);
	return cnt(t->ch[0], v);
}
template <typename T>
void print(node<T> *t) {
	if (t == nullptr) return;
	print(t->ch[0]);
	if (t->ch[0] == nullptr) printf(" %d", t->val);
	print(t->ch[1]);
}

int main()
{
	cin.sync_with_stdio(false);
	int Q, T, X;
	node<int> *root = nullptr;
	cin >> Q;
	while (Q--) {
		cin >> T >> X;
		if (T == 1) {
			root = insert(root, cnt(root, X), X);
		}
		else {
			X--;
			printf("%d\n", find(root, X)->val);
			root = erase(root, X);
		}
	}
	return 0;
}

Submission Info

Submission Time
Task C - データ構造
User kazuma
Language C++14 (GCC 5.4.1)
Score 100
Code Size 5042 Byte
Status AC
Exec Time 312 ms
Memory 183168 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 18
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
subtask1_01.txt AC 1 ms 256 KB
subtask1_02.txt AC 1 ms 256 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 8 ms 4352 KB
subtask1_05.txt AC 18 ms 10624 KB
subtask1_06.txt AC 1 ms 384 KB
subtask1_07.txt AC 156 ms 92544 KB
subtask1_08.txt AC 135 ms 84864 KB
subtask1_09.txt AC 122 ms 74624 KB
subtask1_10.txt AC 280 ms 158208 KB
subtask1_11.txt AC 283 ms 158208 KB
subtask1_12.txt AC 303 ms 145664 KB
subtask1_13.txt AC 312 ms 183168 KB
subtask1_14.txt AC 307 ms 183168 KB
subtask1_15.txt AC 169 ms 115584 KB
subtask1_16.txt AC 192 ms 133760 KB