Submission #1870729
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 *lch, *rch;
int size;
node(T v, node* l = nullptr, node* r = nullptr) : val(v), all(v), lch(l), rch(r), size(1) {}
};
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->lch) + count(t->rch) + 1;
t->all = op(que(t->lch), op(t->val, que(t->rch)));
return t;
}
node *merge(node *l, node *r) {
if (!l || !r) return !l ? r : l;
if (xor128() % (l->size + r->size) < (unsigned)l->size) {
l->rch = merge(l->rch, r);
return update(l);
}
else {
r->lch = merge(l, r->lch);
return update(r);
}
}
pair<node*, node*> split(node *t, int k) {
if (!t) return make_pair(nullptr, nullptr);
if (k <= count(t->lch)) {
pair<node*, node*> s = split(t->lch, k);
t->lch = s.second;
return make_pair(s.first, update(t));
}
else {
pair<node*, node*> s = split(t->rch, k - count(t->lch) - 1);
t->rch = s.first;
return make_pair(update(t), s.second);
}
}
//--------------------------------------options---------------------------------------------------------
node *insert(node *t, int k, T v) {
pair<node*, node*> s = split(t, k);
node *r = merge(s.first, new node(v));
r = merge(r, s.second);
return r;
}
node *erase(node *t, int k) {
pair<node*, node*> s1 = split(t, k), s2 = split(s1.second, 1);
if (s2.first) delete s2.first;
return merge(s1.first, s2.second);
}
node *find(node *t, int k) {
if (!t) return t;
int c = count(t->lch);
return k < c ? find(t->lch, k) : k == c ? t : find(t->rch, k - (c + 1));
}
T find(node *t, int l, int r) {
if (!t) return id;
if (r < 0 || l >= count(t)) return id;
if (l <= 0 && r >= count(t)) return t->all;
int c = count(t->lch);
return op(!t->lch ? id : find(t->lch, l, r), op(l <= c && c < r ? t->val : id, !t->rch ? id : find(t->rch, l - (c + 1), r - (c + 1))));
}
int cnt(node *t, T v) {
if (!t) return 0;
if (t->val < v) return count(t->lch) + 1 + cnt(t->rch, v);
if (t->val == v) return count(t->lch);
return cnt(t->lch, v);
}
void change(node *t, int k, T v) {
if (!t) return;
int c = count(t->lch);
if (k < c) {
change(t->lch, k, v);
update(t);
}
else if (k > c) {
change(t->rch, k - (c + 1), v);
update(t);
}
else {
t->val = v;
update(t);
}
}
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 print(node *t) {
if (!t) return;
print(t->lch);
printf(" %d", t->val);
print(t->rch);
}
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 |
3347 Byte |
Status |
AC |
Exec Time |
275 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 |
7 ms |
256 KB |
subtask1_05.txt |
AC |
16 ms |
384 KB |
subtask1_06.txt |
AC |
1 ms |
256 KB |
subtask1_07.txt |
AC |
138 ms |
3328 KB |
subtask1_08.txt |
AC |
116 ms |
896 KB |
subtask1_09.txt |
AC |
107 ms |
896 KB |
subtask1_10.txt |
AC |
246 ms |
5248 KB |
subtask1_11.txt |
AC |
246 ms |
5248 KB |
subtask1_12.txt |
AC |
275 ms |
9600 KB |
subtask1_13.txt |
AC |
257 ms |
5632 KB |
subtask1_14.txt |
AC |
255 ms |
5632 KB |
subtask1_15.txt |
AC |
86 ms |
5632 KB |
subtask1_16.txt |
AC |
151 ms |
5248 KB |