Submission #2115461
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
template< class Monoid, class OperatorMonoid = Monoid >
struct RandomizedBinarySearchTree
{
using F = function< Monoid(Monoid, Monoid) >;
using G = function< Monoid(Monoid, OperatorMonoid) >;
using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
using P = function< OperatorMonoid(OperatorMonoid, int) >;
inline int xor128()
{
static int x = 123456789;
static int y = 362436069;
static int z = 521288629;
static int w = 88675123;
int t;
t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
struct Node
{
Node *l, *r;
int cnt;
Monoid key, sum;
OperatorMonoid lazy;
Node() {}
Node(const Monoid &k, const OperatorMonoid &p) : cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {}
};
vector< Node > pool;
int ptr;
const Monoid M1;
const OperatorMonoid OM0;
const F f;
const G g;
const H h;
const P p;
RandomizedBinarySearchTree(int sz, const F &f, const Monoid &M1) :
pool(sz), ptr(0), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {}
RandomizedBinarySearchTree(int sz, const F &f, const G &g, const H &h, const P &p,
const Monoid &M1, const OperatorMonoid &OM0) :
pool(sz), ptr(0), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {}
inline Node *alloc(const Monoid &key) { return &(pool[ptr++] = Node(key, OM0)); }
virtual Node *clone(Node *t) { return t; }
inline int count(const Node *t) { return t ? t->cnt : 0; }
inline Monoid sum(const Node *t) { return t ? t->sum : M1; }
inline Node *update(Node *t)
{
t->cnt = count(t->l) + count(t->r) + 1;
t->sum = f(f(sum(t->l), sum(t->r)), t->key);
return t;
}
Node *propagete(Node *t)
{
t = clone(t);
if(t->lazy != OM0) {
t->key = g(t->key, t->lazy);
if(t->l) {
t->l = clone(t->l);
t->l->lazy = h(t->l->lazy, t->lazy);
t->l->sum = f(t->l->sum, p(t->lazy, count(t->l)));
}
if(t->r) {
t->r = clone(t->r);
t->r->lazy = h(t->r->lazy, t->lazy);
t->r->sum = f(t->r->sum, p(t->lazy, count(t->r)));
}
t->lazy = OM0;
}
return update(t);
}
Node *merge(Node *l, Node *r)
{
if(!l || !r) return l ? l : r;
if(xor128() % (l->cnt + r->cnt) < l->cnt) {
l = propagete(l);
l->r = merge(l->r, r);
return update(l);
} else {
r = propagete(r);
r->l = merge(l, r->l);
return update(r);
}
}
pair< Node *, Node * > split(Node *t, int k)
{
if(!t) return {t, t};
t = propagete(t);
if(k <= count(t->l)) {
auto s = split(t->l, k);
t->l = s.second;
return {s.first, update(t)};
} else {
auto s = split(t->r, k - count(t->l) - 1);
t->r = s.first;
return {update(t), s.second};
}
}
Node *build(int l, int r, const vector< Monoid > &v)
{
if(l + 1 >= r) return alloc(v[l]);
return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
}
Node *build(const vector< Monoid > &v)
{
ptr = 0;
return build(0, (int) v.size(), v);
}
void dump(Node *r, typename vector< Monoid >::iterator &it)
{
if(!r) return;
r = propagete(r);
dump(r->l, it);
*it = r->key;
dump(r->r, ++it);
}
vector< Monoid > dump(Node *r)
{
vector< Monoid > v((size_t) count(r));
auto it = begin(v);
dump(r, it);
return v;
}
string to_string(Node *r)
{
auto s = dump(r);
string ret;
for(int i = 0; i < s.size(); i++) ret += ", ";
return (ret);
}
void insert(Node *&t, int k, const Monoid &v)
{
auto x = split(t, k);
t = merge(merge(x.first, alloc(v)), x.second);
}
void erase(Node *&t, int k)
{
auto x = split(t, k);
t = merge(x.first, split(x.second, 1).second);
}
Monoid query(Node *&t, int a, int b)
{
auto x = split(t, a);
auto y = split(x.second, b - a);
auto ret = sum(y.first);
t = merge(x.first, merge(y.first, y.second));
return ret;
}
void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p)
{
auto x = split(t, a);
auto y = split(x.second, b - a);
y.first->lazy = h(y.first->lazy, p);
t = merge(x.first, merge(propagete(y.first), y.second));
}
void set_element(Node *&t, int k, const Monoid &x)
{
t = propagete(t);
if(k < count(t->l)) set_element(t->l, k, x);
else if(k == count(t->l)) t->key = t->sum = x;
else set_element(t->r, k - count(t->l) - 1, x);
t = update(t);
}
};
template< class T >
struct OrderedMultiSet : RandomizedBinarySearchTree< T >
{
using RBST = RandomizedBinarySearchTree< T >;
using Node = typename RBST::Node;
OrderedMultiSet(int sz) : RBST(sz, [&](T x, T y) { return x; }, T()) {}
T kth_element(Node *t, int k)
{
if(k < RBST::count(t->l)) return kth_element(t->l, k);
if(k == RBST::count(t->l)) return t->key;
return kth_element(t->r, k - RBST::count(t->l) - 1);
}
virtual void insert_key(Node *&t, const T &x)
{
RBST::insert(t, lower_bound(t, x), x);
}
void erase_key(Node *&t, const T &x)
{
if(!count(t, x)) return;
RBST::erase(t, lower_bound(t, x));
}
int count(Node *t, const T &x)
{
return upper_bound(t, x) - lower_bound(t, x);
}
int lower_bound(Node *t, const T &x)
{
if(!t) return 0;
if(x <= t->key) return lower_bound(t->l, x);
return lower_bound(t->r, x) + RBST::count(t->l) + 1;
}
int upper_bound(Node *t, const T &x)
{
if(!t) return 0;
if(x < t->key) return upper_bound(t->l, x);
return upper_bound(t->r, x) + RBST::count(t->l) + 1;
}
int size(Node *t)
{
return RBST::count(t);
}
bool empty(Node *t)
{
return !t;
}
Node *makeset()
{
return (nullptr);
}
};
template< class T >
struct OrderedSet : OrderedMultiSet< T >
{
using SET = OrderedMultiSet< T >;
using RBST = typename SET::RBST;
using Node = typename RBST::Node;
OrderedSet(int sz) : OrderedMultiSet< T >(sz) {}
void insert_key(Node *&t, const T &x) override
{
if(SET::count(t, x)) return;
RBST::insert(t, SET::lower_bound(t, x), x);
}
};
const int INF = 1 << 30;
int main()
{
int Q;
scanf("%d", &Q);
OrderedSet< int > beet(Q);
auto root = beet.makeset();
while(Q--) {
int T, X;
scanf("%d %d", &T, &X);
if(T == 1) {
beet.insert_key(root, X);
} else {
auto ret = beet.kth_element(root, --X);
printf("%d\n", ret);
beet.erase_key(root, ret);
}
}
}
Submission Info
Submission Time |
|
Task |
C - データ構造 |
User |
ei13333 |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
6888 Byte |
Status |
AC |
Exec Time |
393 ms |
Memory |
6528 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:280:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
^
./Main.cpp:286:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &T, &X);
^
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 |
11 ms |
512 KB |
subtask1_05.txt |
AC |
27 ms |
768 KB |
subtask1_06.txt |
AC |
2 ms |
256 KB |
subtask1_07.txt |
AC |
222 ms |
3712 KB |
subtask1_08.txt |
AC |
204 ms |
4480 KB |
subtask1_09.txt |
AC |
187 ms |
4352 KB |
subtask1_10.txt |
AC |
393 ms |
5888 KB |
subtask1_11.txt |
AC |
381 ms |
5760 KB |
subtask1_12.txt |
AC |
389 ms |
6528 KB |
subtask1_13.txt |
AC |
391 ms |
4480 KB |
subtask1_14.txt |
AC |
384 ms |
5632 KB |
subtask1_15.txt |
AC |
137 ms |
5376 KB |
subtask1_16.txt |
AC |
239 ms |
5760 KB |