Submission #1870740
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
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 = int;
const T id = INT_MAX;
T op(T l, T r) {
return min(l, r);
}
struct node {
T val, all;
node *ch[2];
unsigned int pri;
int size;
node(T v, unsigned int p, node* l = nullptr, node* r = nullptr) : val(v), all(v), pri(p), size(1) {
ch[0] = l; ch[1] = r;
}
};
int count(node *t) { return !t ? 0 : t->size; }
T que(node *t) { return !t ? id : t->all; }
node *update(node *t) {
t->size = count(t->ch[0]) + count(t->ch[1]) + 1;
t->all = op(que(t->ch[0]), op(t->val, que(t->ch[1])));
return t;
}
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 *insert(node *t, int k, T v, unsigned int pri) {
if (!t) return new node(v, pri);
int c = count(t->ch[0]), b = (k > c);
t->ch[b] = insert(t->ch[b], k - (b ? (c + 1) : 0), v, pri);
update(t);
if (t->pri > t->ch[b]->pri) t = rotate(t, 1 - b);
return t;
}
node *erase(node *t) {
if (!t) return nullptr;
if (!t->ch[0] && !t->ch[1]) {
delete t;
return nullptr;
}
int b = (t->ch[0] && t->ch[1]) ? (t->ch[0]->pri > t->ch[1]->pri) : (t->ch[0] != nullptr);
t = rotate(t, b);
t->ch[b] = erase(t->ch[b]);
update(t);
return t;
}
node *erase(node *t, int k) {
if (!t) return nullptr;
int c = count(t->ch[0]);
if (k < c) {
t->ch[0] = erase(t->ch[0], k);
update(t);
}
else if (k > c) {
t->ch[1] = erase(t->ch[1], k - (c + 1));
update(t);
}
else {
t = erase(t);
}
return t;
}
node *merge(node *l, node *r) {
node *p = new node(0, 0);
p->ch[0] = l, p->ch[1] = r;
p = erase(p);
return p;
}
pair<node*, node*> split(node *t, int k) {
if (!t) return make_pair(nullptr, nullptr);
node *p = insert(t, k, 0, 0);
auto res = make_pair(p->ch[0], p->ch[1]);
delete p;
return res;
}
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 k < c ? find(t->ch[0], k) : k == c ? t : find(t->ch[1], k - (c + 1));
}
int cnt(node *t, T v) {
if (!t) return 0;
if (t->val < v) return count(t->ch[0]) + 1 + cnt(t->ch[1], v);
if (t->val == v) return count(t->ch[0]);
return cnt(t->ch[0], v);
}
void print(node *t) {
if (!t) return;
print(t->ch[0]);
printf(" %d", t->val);
print(t->ch[1]);
}
node *shift(node *t, int l, int r) {
pair<node*, node*> s1 = split(t, l), s2 = split(s1.second, r - l), s3 = split(s2.second, 1);
return merge(s1.first, merge(s3.first, merge(s2.first, s3.second)));
}
void change(node *t, int k, T v) {
if (!t) return;
int c = count(t->ch[0]);
if (k < c) {
change(t->ch[0], k, v);
update(t);
}
else if (k > c) {
change(t->ch[1], k - (c + 1), v);
update(t);
}
else {
t->val = v;
update(t);
}
}
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, xor128());
}
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 |
3695 Byte |
Status |
AC |
Exec Time |
279 ms |
Memory |
9600 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 |
6 ms |
256 KB |
subtask1_05.txt |
AC |
13 ms |
384 KB |
subtask1_06.txt |
AC |
1 ms |
256 KB |
subtask1_07.txt |
AC |
95 ms |
3328 KB |
subtask1_08.txt |
AC |
87 ms |
896 KB |
subtask1_09.txt |
AC |
82 ms |
896 KB |
subtask1_10.txt |
AC |
173 ms |
5248 KB |
subtask1_11.txt |
AC |
172 ms |
5248 KB |
subtask1_12.txt |
AC |
192 ms |
9600 KB |
subtask1_13.txt |
AC |
279 ms |
5632 KB |
subtask1_14.txt |
AC |
265 ms |
5632 KB |
subtask1_15.txt |
AC |
71 ms |
5632 KB |
subtask1_16.txt |
AC |
109 ms |
5248 KB |