This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2890
#include "bbst/implicit_treap.hpp"
int main() {
int n, q;
scanf("%d%d", &n, &q);
auto prop = [&](auto &n, auto &v) {
if (!v.second) return;
swap(n.l, n.r);
if (n.l) n.l->val().second ^= true;
if (n.r) n.r->val().second ^= true;
v.second = false;
};
implicit_treap<pair<int, bool>, no_op, decltype(prop)> tree(no_op{}, prop);
vector<decltype(tree.begin())> its(n+1);
rep(i, n) {
int a; scanf("%d", &a);
its[a] = tree.insert(i, {a, false});
}
bool flip = q % 2;
while (q--) {
int x; scanf("%d", &x);
int k = tree.index_of(its[x]);
tree.apply_on(0, k, [](auto &v) { v.second ^= true; });
tree.apply_on(k + 1, n, [](auto &v) { v.second ^= true; });
}
if (flip) tree.root().second ^= true;
tree.propagate_all();
decltype(tree)::const_iterator it = tree.begin();
printf("%d", it++->first);
while (it != tree.end()) printf(" %d", it++->first);
printf("\n");
}
#line 1 "test/ds/implicit_treap.index_of.test.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2890
#line 2 "bbst/implicit_treap.hpp"
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "bbst/iterator.hpp"
template <class Self, class Node, class T, bool Const>
class bidirectional_iterator_impl {
public:
using difference_type = ptrdiff_t;
using value_type = T;
using pointer = conditional_t<Const, const T *, T *>;
using reference = conditional_t<Const, const T &, T &>;
using iterator_category = bidirectional_iterator_tag;
private:
using self_type = Self;
using node_pointer = conditional_t<Const, const Node *, Node *>;
public:
bidirectional_iterator_impl(Node *p) : ptr(p) { }
bidirectional_iterator_impl(const Node *p = nullptr) : ptr(p) {} // ill-formed if !Const
// reference operator*() const { return ptr->*Val(); }
// pointer operator->() const { return &**this; }
bool operator==(self_type rhs) const { return ptr == rhs.ptr; }
bool operator!=(self_type rhs) const { return ptr != rhs.ptr; }
self_type &operator++() {
if (ptr->r) {
ptr = ptr->r;
while (ptr->l) ptr = ptr->l;
} else {
while (ptr->par->r == ptr) ptr = ptr->par;
ptr = ptr->par;
}
return *(self_type *)this;
}
self_type operator++(int) {
self_type res(ptr);
++*this;
return res;
}
self_type &operator--() {
if (ptr->l) {
ptr = ptr->l;
while (ptr->r) ptr = ptr->r;
} else {
while (ptr->par->l == ptr) ptr = ptr->par;
ptr = ptr->par;
}
return *(self_type *)this;
}
self_type operator--(int) {
self_type res(ptr);
--*this;
return res;
}
node_pointer ptr;
};
#line 3 "bit/clz.hpp"
#pragma GCC target("lzcnt")
template <class T>
int clz(T x) {
if (!x) return sizeof(T) * 8;
if constexpr (sizeof(T) <= sizeof(unsigned)) {
return __builtin_clz((unsigned)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
return __builtin_clzll((unsigned long long)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
}
}
#line 4 "bit/ilog2.hpp"
template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }
template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }
template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }
template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 3 "func/no_op.hpp"
struct no_op {
template <class... Ts>
void operator()(Ts&&...) const {}
};
#line 3 "util/pool.hpp"
template <size_t N = 1 << 20>
struct pool {
template <class T>
struct alloc {
static inline T* ptr =
(T*)new (align_val_t(alignof(T))) unsigned char[sizeof(T) * N];
static void* operator new(size_t) noexcept { return ptr++; }
static void operator delete(void* p) { destroy_at((T*)p); }
private:
alloc() = default;
friend T;
};
};
struct no_pool {
template <class T>
struct alloc {};
};
#line 3 "util/seed.hpp"
auto seed() {
#if defined(LOCAL) && !defined(NO_FIX_SEED)
return 314169265258979;
#endif
return chrono::steady_clock::now().time_since_epoch().count();
}
#line 3 "util/rand.hpp"
uint32_t rand32() {
static uint32_t x = seed();
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
uint64_t rand64() {
return uint64_t(rand32()) << 32 | rand32();
}
#line 8 "bbst/implicit_treap.hpp"
template <
class T, class Update = no_op, class Propagate = no_op,
class Alloc = pool<>>
class implicit_treap {
class node;
class node_base;
using link = node_base*;
using clink = const node_base*;
class node_base {
public:
link l = nullptr, r = nullptr, par = nullptr;
int cnt = 1;
uint32_t key = rand32();
T& val() { return ((node*)this)->val; }
const T& val() const { return ((const node*)this)->val; }
};
class node : public node_base, public Alloc::template alloc<node> {
public:
T val;
node(T val) : val(move(val)) { }
};
private:
public:
class iterator
: public bidirectional_iterator_impl<iterator, node_base, T, false> {
using base_type =
bidirectional_iterator_impl<iterator, node_base, T, false>;
public:
using base_type::base_type;
T& operator*() const { return base_type::ptr->val(); }
T* operator->() const { return &**this; }
};
class const_iterator
: public bidirectional_iterator_impl<const_iterator, node_base, T, true> {
using base_type =
bidirectional_iterator_impl<const_iterator, node_base, T, true>;
public:
using base_type::base_type;
const_iterator(iterator it) : base_type(it.ptr) { }
const T& operator*() const { return base_type::ptr->val(); }
const T* operator->() const { return &**this; }
};
public:
implicit_treap(Update upd = Update(), Propagate prop = Propagate())
: upd(upd), prop(prop) { }
template <class It>
implicit_treap(
It first, It last, Update upd = Update(), Propagate prop = Propagate())
: upd(upd), prop(prop) {
vector<link> ps(bit_ceil(distance(first, last)) * 2);
for (auto it = ps.begin(); first != last; it++, first++)
*it = (link) new node(*first);
for (int i = 0; i + 1 < ps.size(); i += 2)
ps.push_back(merge(ps[i], ps[i + 1]));
set_l(&head, ps.back());
}
implicit_treap(const implicit_treap& rhs) : upd(rhs.upd), prop(rhs.prop) {
set_l(&head, deep_copy(rhs.head.l));
}
implicit_treap(implicit_treap&& rhs) : upd(rhs.upd), prop(rhs.prop) {
set_l(&head, rhs.head.l);
rhs.head.l = nullptr;
}
implicit_treap& operator=(const implicit_treap& rhs) {
set_l(&head, deep_copy(rhs.head.l));
return *this;
}
implicit_treap& operator=(implicit_treap&& rhs) {
set_l(&head, rhs.head.l), rhs.head.l = nullptr;
return *this;
}
const_iterator begin() const {
auto ptr = &head;
while (ptr->l) ptr = ptr->l;
return const_iterator(ptr);
}
const_iterator end() const { return const_iterator(&head); }
const T& root() const { return head.l->val(); }
T& root() { return head.l->val(); }
const_iterator find(int k) const { return const_iterator(find(head.l, k)); }
template <class F>
void apply_on(int i, int j, F f) {
if (i == j) return;
auto [lm, r] = split(j);
auto [l, m] = lm.split(i);
f(m.root());
*this = move(l.join(m).join(r));
}
int size() const { return count(head.l); }
implicit_treap& join(implicit_treap& rhs) {
set_l(&head, merge(head.l, rhs.head.l));
rhs.head.l = nullptr;
return *this;
}
pair<implicit_treap, implicit_treap> split(int k) {
auto [l, r] = split(head.l, k);
implicit_treap left(l, upd, prop);
set_l(&head, r);
return pair(move(left), move(*this));
}
const_iterator insert(int k, T v) {
auto p = (link) new node(move(v));
set_l(&head, insert(head.l, k, p));
return const_iterator(p);
}
void erase(int i) { set_l(&head, erase(head.l, i)); }
int index_of(const_iterator it) {
auto p = const_cast<link>(it.ptr);
propagate(*p);
int ans = count(p->l);
for (; p->par != &head; p = p->par) {
if (p->par->r == p) ans += count(p->par->l) + 1;
auto l0 = p->par->l;
propagate(*p->par);
if (l0 != p->par->l) ans = count(p->par) - ans - 1;
}
return ans;
}
void propagate_all() { propagate_all(head.l); }
private:
implicit_treap(link root, Update upd, Propagate prop) : upd(upd), prop(prop) {
set_l(&head, root);
}
node_base head;
Update upd;
Propagate prop;
int count(link p) const { return p ? p->cnt : 0; }
void propagate(node_base& n) { prop(n, n.val()); }
void update(node_base& n) { update((node&)n); }
void update(node& n) {
n.cnt = 1 + count(n.l) + count(n.r);
if (n.l) propagate(*n.l);
if (n.r) propagate(*n.r);
upd((node_base&)n, n.val);
}
void set_l(link par, link p) {
par->l = p;
if (p) p->par = par;
}
void set_r(link par, link p) {
par->r = p;
if (p) p->par = par;
}
link merge(link l, link r) {
if (!l) return r;
if (!r) return l;
if (l->key > r->key) {
return propagate(*l), set_r(l, merge(l->r, r)), update(*l), l;
} else {
return propagate(*r), set_l(r, merge(l, r->l)), update(*r), r;
}
}
pair<link, link> split(link p, int k) {
if (!p) {
assert(k == 0);
return pair(nullptr, nullptr);
}
propagate(*p);
if (count(p->l) < k) {
auto [l, r] = split(p->r, k - count(p->l) - 1);
set_r(p, l), update(*p);
return pair(p, r);
} else {
auto [l, r] = split(p->l, k);
set_l(p, r), update(*p);
return pair(l, p);
}
}
link insert(link p, int k, link x) {
if (!p) {
assert(k == 0);
return x;
}
propagate(*p);
if (x->key > p->key) {
auto [l, r] = split(p, k);
return set_l(x, l), set_r(x, r), update(*x), x;
} else if (count(p->l) < k) {
return set_r(p, insert(p->r, k - count(p->l) - 1, x)), update(*p), p;
} else {
return set_l(p, insert(p->l, k, x)), update(*p), p;
}
}
link erase(link p, int k) {
assert(p);
propagate(*p);
if (count(p->l) < k) {
return set_r(p, erase(p->r, k - count(p->l) - 1)), update(*p), p;
} else if (count(p->l) > k) {
return set_l(p, erase(p->l, k)), update(*p), p;
} else {
return merge(p->l, p->r);
}
}
link find(link p, int k) const {
return count(p->l) == k ? p
: count(p->l) < k ? find(p->r, k - count(p->l) - 1)
: find(p->l, k);
}
link deep_copy(link p) {
if (!p) return nullptr;
link q = (link) new node(*(node*)p);
set_l(q, deep_copy(p->l));
set_r(q, deep_copy(p->r));
return q;
}
void propagate_all(link p) {
if (!p) return;
propagate(*p);
propagate_all(p->l), propagate_all(p->r);
}
};
#line 4 "test/ds/implicit_treap.index_of.test.cpp"
int main() {
int n, q;
scanf("%d%d", &n, &q);
auto prop = [&](auto &n, auto &v) {
if (!v.second) return;
swap(n.l, n.r);
if (n.l) n.l->val().second ^= true;
if (n.r) n.r->val().second ^= true;
v.second = false;
};
implicit_treap<pair<int, bool>, no_op, decltype(prop)> tree(no_op{}, prop);
vector<decltype(tree.begin())> its(n+1);
rep(i, n) {
int a; scanf("%d", &a);
its[a] = tree.insert(i, {a, false});
}
bool flip = q % 2;
while (q--) {
int x; scanf("%d", &x);
int k = tree.index_of(its[x]);
tree.apply_on(0, k, [](auto &v) { v.second ^= true; });
tree.apply_on(k + 1, n, [](auto &v) { v.second ^= true; });
}
if (flip) tree.root().second ^= true;
tree.propagate_all();
decltype(tree)::const_iterator it = tree.begin();
printf("%d", it++->first);
while (it != tree.end()) printf(" %d", it++->first);
printf("\n");
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-12 | 00_sample_00.in |
![]() |
19 ms | 13 MB |
g++-12 | 00_sample_01.in |
![]() |
19 ms | 13 MB |
g++-12 | 10_random_00.in |
![]() |
529 ms | 15 MB |
g++-12 | 10_random_01.in |
![]() |
275 ms | 13 MB |
g++-12 | 10_random_02.in |
![]() |
198 ms | 15 MB |
g++-12 | 10_random_03.in |
![]() |
449 ms | 13 MB |
g++-12 | 10_random_04.in |
![]() |
394 ms | 13 MB |
g++-12 | 10_random_05.in |
![]() |
484 ms | 13 MB |
g++-12 | 10_random_06.in |
![]() |
504 ms | 13 MB |
g++-12 | 10_random_07.in |
![]() |
330 ms | 15 MB |
g++-12 | 10_random_08.in |
![]() |
117 ms | 13 MB |
g++-12 | 10_random_09.in |
![]() |
349 ms | 13 MB |
g++-12 | 10_random_10.in |
![]() |
146 ms | 13 MB |
g++-12 | 10_random_11.in |
![]() |
344 ms | 13 MB |
g++-12 | 10_random_12.in |
![]() |
183 ms | 13 MB |
g++-12 | 10_random_13.in |
![]() |
425 ms | 13 MB |
g++-12 | 10_random_14.in |
![]() |
135 ms | 15 MB |
g++-12 | 10_random_15.in |
![]() |
139 ms | 13 MB |
g++-12 | 10_random_16.in |
![]() |
309 ms | 15 MB |
g++-12 | 10_random_17.in |
![]() |
155 ms | 15 MB |
g++-12 | 10_random_18.in |
![]() |
258 ms | 15 MB |
g++-12 | 10_random_19.in |
![]() |
186 ms | 13 MB |
g++-12 | 10_random_20.in |
![]() |
295 ms | 15 MB |
g++-12 | 10_random_21.in |
![]() |
216 ms | 13 MB |
g++-12 | 10_random_22.in |
![]() |
96 ms | 13 MB |
g++-12 | 10_random_23.in |
![]() |
332 ms | 13 MB |
g++-12 | 10_random_24.in |
![]() |
384 ms | 13 MB |
g++-12 | 10_random_25.in |
![]() |
359 ms | 13 MB |
g++-12 | 10_random_26.in |
![]() |
212 ms | 13 MB |
g++-12 | 10_random_27.in |
![]() |
230 ms | 13 MB |
g++-12 | 10_random_28.in |
![]() |
99 ms | 13 MB |
g++-12 | 10_random_29.in |
![]() |
172 ms | 13 MB |
g++-12 | 20_corner_00.in |
![]() |
19 ms | 13 MB |
g++-12 | 20_corner_01.in |
![]() |
19 ms | 13 MB |
g++-12 | 20_corner_02.in |
![]() |
404 ms | 15 MB |