Submission #336525

#TimeUsernameProblemLanguageResultExecution timeMemory
336525Return_0Klasika (COCI20_klasika)C++17
110 / 110
3222 ms439328 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx,avx2,fma")

using namespace std;
using namespace __gnu_pbds;

typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1);
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< x << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()

const long long inf = 1e18;
cl mod = 1e9 + 7, MOD = 998244353;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
    return out << '(' << a.first << ", " << a.second << ')';    }
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
    if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

cl N = 2e5 + 7, lg = 30;

struct node
{
    set <ll> st;
    ll bit;
    node *zero, *one;

    node (ll _bit = 30) {
        bit = _bit;
        st.clear();
        zero = nullptr;
        one = nullptr;
    }

    void add (ll num, ll pos) {
        st.insert(pos);
        if (bit < 0)   return;
        if (num >> bit & 1) {
            if (one == nullptr) one = new node (bit - 1);
            one->add(num, pos);
        } else {
            if (zero == nullptr) zero = new node (bit - 1);
            zero->add(num, pos);
        }
    }

    ll ask (ll num, ll ql, ll qr) {
        if (bit < 0)   return 0;
        if (num >> bit & 1) {
            if (zero != nullptr) {
                auto it = zero->st.lower_bound(ql);
                if (it != zero->st.end() && *it <= qr)  return (1 << bit) + zero->ask(num, ql, qr);
            }
            /* if (one != nullptr) */    return one->ask(num, ql, qr);
            // else return 0;
        } else {
            if (one != nullptr) {
                auto it = one->st.lower_bound(ql);
                if (it != one->st.end() && *it <= qr)  return (1 << bit) + one->ask(num, ql, qr);
            }
            /* if (zero != nullptr) */    return zero->ask(num, ql, qr);
            // else return 0;
        }
    }
} Trie;

ll tin [N], tout [N], T, h [N];
vector <pll> adj [N];
vector <pair <bool, pll>> Q;

void dfs (ll v = 1, ll cur = 0) {
    tin[v] = ++T;
    h[v] = cur;
    for (auto u : adj[v])   dfs(u.fr, cur ^ u.sc);
    tout[v] = T;
}

int main ()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll n = 1, q, i, j, x, y;
    string tp;

    cin>> q;

    for (i = 0; i < q; i++) {
        cin>> tp >> x >> y;
        if (SZ(tp) == 3) {
            n++;
            Q.push_back({0, {x, n}});
            adj[x].push_back({n, y});
        }
        else Q.push_back({1, {x, y}});
    }

    dfs();

    Trie.add(0, 1);
    for (auto p : Q) {
        if (!p.fr)  Trie.add(h[p.sc.sc], tin[p.sc.sc]);
        else cout<< Trie.ask(h[p.sc.fr], tin[p.sc.sc], tout[p.sc.sc]) << '\n';
    }

    return 0;
}
/*
2
Add 1 92
Query 2 1

10
Add 1 4
Add 1 9
Add 1 10
Add 2 2
Add 3 3
Add 4 4
Query 4 2
Query 1 3
Add 6 7
Query 1 3

6
Add 1 5
Add 2 7
Add 1 4
Add 4 3
Query 1 1
Query 2 4

4
Add 1 5
Query 1 1
Add 1 7
Query 1 1


*/

Compilation message (stderr)

klasika.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)':
klasika.cpp:44:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   44 |     if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
      |     ^~
klasika.cpp:44:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   44 |     if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
      |                                    ^~~~
klasika.cpp: In function 'int main()':
klasika.cpp:109:21: warning: unused variable 'j' [-Wunused-variable]
  109 |     ll n = 1, q, i, j, x, y;
      |                     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...