Submission #1870819
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using T = int;
const T id = INT_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, int 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);
}
T find(node *t, int l, int r) {
if (!t) 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 op(!t->ch[0] ? id : find(t->ch[0], l, r), op(l <= c && c < r ? t->val : id, !t->ch[1] ? id : find(t->ch[1], l - (c + 1), r - (c + 1))));
}
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);
}
void print(node *t) {
if (!t) return;
print(t->ch[0]);
if (!t->ch[0]) printf(" %d", t->val);
print(t->ch[1]);
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int Q, T, X;
node *root = nullptr;
cin >> Q;
while (Q--) {
cin >> T >> X;
if (T == 1) {
root = insert(root, cnt(root, X), X);
}
else {
node *t = find(root, X - 1);
printf("%d\n", t ? t->val : 0);
root = erase(root, X - 1);
}
}
return 0;
}
Submission Info
Submission Time |
|
Task |
C - データ構造 |
User |
kazuma |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
4361 Byte |
Status |
AC |
Exec Time |
337 ms |
Memory |
183168 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
100 / 100 |
Status |
|
|
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 |
163 ms |
92544 KB |
subtask1_08.txt |
AC |
138 ms |
84864 KB |
subtask1_09.txt |
AC |
122 ms |
74624 KB |
subtask1_10.txt |
AC |
290 ms |
158208 KB |
subtask1_11.txt |
AC |
301 ms |
158208 KB |
subtask1_12.txt |
AC |
308 ms |
145664 KB |
subtask1_13.txt |
AC |
337 ms |
183168 KB |
subtask1_14.txt |
AC |
315 ms |
183168 KB |
subtask1_15.txt |
AC |
171 ms |
115584 KB |
subtask1_16.txt |
AC |
198 ms |
133760 KB |