#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector sort_unique(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl #define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr) #else #define dbg(x) (x) #define dbgif(cond, x) 0 #endif // Lazy randomized binary search tree template struct lazy_rbst { // Do your RuBeSTy! ⌒°( ・ω・)°⌒ inline uint32_t _rand() { // XorShift static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } struct Node { Node *l, *r; S val, sum; F lz; bool is_reversed; int sz; Node(const S &v) : l(nullptr), r(nullptr), val(v), sum(v), lz(id()), is_reversed(false), sz(1) {} Node() : l(nullptr), r(nullptr), lz(id()), is_reversed(false), sz(0) {} template friend OStream &operator<<(OStream &os, const Node &n) { os << '['; if (n.l) os << *(n.l) << ','; os << n.val << ','; if (n.r) os << *(n.r); return os << ']'; } }; using Nptr = Node *; std::array data; int d_ptr; int size(Nptr t) const { return t != nullptr ? t->sz : 0; } lazy_rbst() : d_ptr(0) {} protected: Nptr update(Nptr t) { t->sz = 1; t->sum = t->val; if (t->l) { t->sz += t->l->sz; t->sum = op(t->l->sum, t->sum); } if (t->r) { t->sz += t->r->sz; t->sum = op(t->sum, t->r->sum); } return t; } void all_apply(Nptr t, F f) { t->val = mapping(f, t->val); t->sum = mapping(f, t->sum); t->lz = composition(f, t->lz); } void _toggle(Nptr t) { auto tmp = t->l; t->l = t->r, t->r = tmp; t->sum = reversal(t->sum); t->is_reversed ^= true; } void push(Nptr &t) { _duplicate_node(t); if (t->lz != id()) { if (t->l) { _duplicate_node(t->l); all_apply(t->l, t->lz); } if (t->r) { _duplicate_node(t->r); all_apply(t->r, t->lz); } t->lz = id(); } if (t->is_reversed) { if (t->l) _toggle(t->l); if (t->r) _toggle(t->r); t->is_reversed = false; } } virtual void _duplicate_node(Nptr &) {} Nptr _make_node(const S &val) { if (d_ptr >= LEN) throw; return &(data[d_ptr++] = Node(val)); } public: Nptr new_tree() { return nullptr; } // 新たな木を作成 int mem_used() const { return d_ptr; } bool empty(Nptr t) const { return t == nullptr; } // lとrをrootとする木同士を結合して,新たなrootを返す Nptr merge(Nptr l, Nptr r) { if (l == nullptr or r == nullptr) return l != nullptr ? l : r; if (_rand() % uint32_t(l->sz + r->sz) < uint32_t(l->sz)) { push(l); l->r = merge(l->r, r); return update(l); } else { push(r); r->l = merge(l, r->l); return update(r); } } // [0, k)の木と[k, root->size())の木に分けて各root // (部分木の要素数が0ならnullptr)を返す std::pair split(Nptr &root, int k) { // rootの子孫からあとk個欲しい if (root == nullptr) return std::make_pair(nullptr, nullptr); push(root); if (k <= size(root->l)) { // leftからk個拾える auto p = split(root->l, k); root->l = p.second; return std::make_pair(p.first, update(root)); } else { auto p = split(root->r, k - size(root->l) - 1); root->r = p.first; return std::make_pair(update(root), p.second); } } // 0-indexedでarray[pos]の手前に新たな要素 x を挿入する void insert(Nptr &root, int pos, const S &x) { auto p = split(root, pos); root = merge(p.first, merge(_make_node(x), p.second)); } // 0-indexedでarray[pos]を削除する(先頭からpos+1個目の要素) void erase(Nptr &root, int pos) { auto p = split(root, pos); auto p2 = split(p.second, 1); root = merge(p.first, p2.second); } // 1点更新 array[pos].valにupdvalを入れる void set(Nptr &root, int pos, const S &x) { auto p = split(root, pos); auto p2 = split(p.second, 1); _duplicate_node(p2.first); *p2.first = Node(x); root = merge(p.first, merge(p2.first, p2.second)); } // 遅延評価を利用した範囲更新 [l, r) void apply(Nptr &root, int l, int r, const F &f) { if (l == r) return; auto p = split(root, l); auto p2 = split(p.second, r - l); all_apply(p2.first, f); root = merge(p.first, merge(p2.first, p2.second)); } S prod(Nptr &root, int l, int r) { assert(l < r); auto p = split(root, l); auto p2 = split(p.second, r - l); if (p2.first != nullptr) push(p2.first); S res = p2.first->sum; root = merge(p.first, merge(p2.first, p2.second)); return res; } // array[pos].valを取得する S get(Nptr &root, int pos) { return prod(root, pos, pos + 1); } template int max_right(Nptr root, const S &e) { return max_right(root, e, [](S x) { return g(x); }); } template int max_right(Nptr root, const S &e, G g) { assert(g(e)); if (root == nullptr) return 0; push(root); Nptr now = root; S prod_now = e; int sz = 0; while (true) { if (now->l != nullptr) { push(now->l); auto pl = op(prod_now, now->l->sum); if (g(pl)) { prod_now = pl; sz += now->l->sz; } else { now = now->l; continue; } } auto pl = op(prod_now, now->val); if (!g(pl)) return sz; prod_now = pl, sz++; if (now->r == nullptr) return sz; push(now->r); now = now->r; } } template int min_left(Nptr root, const S &e) { return min_left(root, e, [](S x) { return g(x); }); } template int min_left(Nptr root, const S &e, G g) { assert(g(e)); if (root == nullptr) return 0; push(root); Nptr now = root; S prod_now = e; int sz = size(root); while (true) { if (now->r != nullptr) { push(now->r); auto pr = op(now->r->sum, prod_now); if (g(pr)) { prod_now = pr; sz -= now->r->sz; } else { now = now->r; continue; } } auto pr = op(now->val, prod_now); if (!g(pr)) return sz; prod_now = pr, sz--; if (now->l == nullptr) return sz; push(now->l); now = now->l; } } void reverse(Nptr &root) { _duplicate_node(root), _toggle(root); } void reverse(Nptr &root, int l, int r) { auto p2 = split(root, r); auto p1 = split(p2.first, l); reverse(p1.second); root = merge(merge(p1.first, p1.second), p2.second); } // データを壊して新規にinitの内容を詰める void assign(Nptr &root, const std::vector &init) { int N = init.size(); root = N ? _assign_range(0, N, init) : new_tree(); } Nptr _assign_range(int l, int r, const std::vector &init) { if (r - l == 1) { Nptr t = _make_node(init[l]); return update(t); } return merge(_assign_range(l, (l + r) / 2, init), _assign_range((l + r) / 2, r, init)); } // データをvecへ書き出し void dump(Nptr &t, std::vector &vec) { if (t == nullptr) return; push(t); dump(t->l, vec); vec.push_back(t->val); dump(t->r, vec); } // gc void re_alloc(Nptr &root) { std::vector mem; dump(root, mem); d_ptr = 0; assign(root, mem); } }; using S = pint; // [maxcnt, key] S op(S l, S r) { return {max(l.first, r.first), max(l.second, r.second)}; } using F = int; S reversal(S x) { return x; } S mapping(F f, S x) { return {x.first + f, x.second}; } F compositon(F f, F g) { return f + g; } F id() { return 0; } int main() { int N; cin >> N; lazy_rbst<400000, S, op, F, reversal, mapping, compositon, id> rbst; auto root = rbst.new_tree(); int ret; cin >> ret; cout << ret << '\n'; vector A(N); A[0] = ret; rbst.insert(root, 0, S{1, ret}); set se; se.insert(ret); FOR(i, 1, N) { int an; cin >> an; an ^= ret; A[i] = an; auto k = rbst.max_right(root, pint(-1, -1), [&](S x) { return x.second <= A[i]; }); auto [ltree, rtree] = rbst.split(root, k); if (se.count(an)) { auto v = rbst.get(ltree, rbst.size(ltree) - 1); int vv = max(0, v.first); if (rbst.size(rtree)) chmax(vv, rbst.get(rtree, 0).first); rbst.set(ltree, rbst.size(ltree) - 1, {vv, v.second}); } else { int vv = 0; if (rbst.size(rtree)) chmax(vv, rbst.get(rtree, 0).first); rbst.insert(ltree, rbst.size(ltree), S{vv, A[i]}); } rbst.apply(rtree, 0, rbst.size(rtree), -1); rbst.apply(ltree, 0, rbst.size(ltree), 1); root = rbst.merge(ltree, rtree); se.insert(an); k = rbst.min_left(root, pint(-1, -1), [&](S x) { return x.first <= 0; }) - 1; dbg(*root); dbg(k); ret = rbst.get(root, k).second; cout << ret << '\n'; } dbg(A); }