Submission #1312808

#TimeUsernameProblemLanguageResultExecution timeMemory
1312808crazycoder00Klasika (COCI20_klasika)C++20
0 / 110
504 ms117352 KiB
#include <bits/stdc++.h> using namespace std; // Template // ============================================== // pbds // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template<typename T, typename comp = less<T>> // using ordered_set = tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>; // Debugging #ifdef LOCAL #include "debug.h" #else #define debug(...) #define see(x) #endif typedef long long ll; typedef vector<int> VI; typedef vector<long long> VLL; typedef vector<bool> VB; typedef vector<vector<int>> VVI; typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<pair<int, int>> VPI; #define pb push_back #define ff first #define ss second #define mp make_pair #define all(a) a.begin(), a.end() #define revall(a) a.rbegin(), a.rend() #define loop(i, s, e) for (int i = s; i < e; ++i) #define inp(v) for (auto& x : v) cin >> x #define outp(v, st) for (int i = st, n = v.size(); i < n; ++i) cout << v[i] << " \n"[i == n - 1] #define nl "\n" #define yep cout << "YES\n" #define nope cout << "NO\n" #define INF (int) 1e9 #define INFL (ll) 1e18 #define MOD 998244353 // #define MOD 1000000007 #define MAXN 2000002 // ============================================= struct LCA { int step = 0; VI vis_start, vis_end; VVI st; int k; VVI& tree; LCA(VVI& tree) : tree(tree) { init(tree.size() - 1); } void build_st(int u, int p) { st[u][0] = p; for (int i = 1; i <= k; ++i) st[u][i] = st[st[u][i - 1]][i - 1]; } void dfs(int u, int p) { vis_start[u] = step++; build_st(u, p); for (auto v : tree[u]) { if (v != p) dfs(v, u); } vis_end[u] = step++; } bool is_ancestor(int a, int b) { return vis_start[a] < vis_start[b] && vis_end[a] > vis_end[b]; } int get(int a, int b) { if (a == b) return a; if (is_ancestor(a, b)) return a; if (is_ancestor(b, a)) return b; for (int i = k; i >= 0; --i) { if (!is_ancestor(st[a][i], b)) a = st[a][i]; } return st[a][0]; } void init(int n) { vis_start.assign(n + 1, 0); vis_end.assign(n + 1, 0); k = __lg(2 * n - 1); st.assign(n + 1, VI(k + 1)); dfs(1, 1); } }; struct trienode { int child[2]; set<int> tm; trienode() { child[0] = child[1] = 0; } }; struct trie { vector<trienode> nodes; trie() { nodes.pb(trienode()); } void insert(int x, int disc) { int cur = 0; for (int i = 30; i >= 0; --i) { int nxt = (bool) (x & (1 << i)); if (!nodes[cur].child[nxt]) { nodes[cur].child[nxt] = nodes.size(); nodes.pb(trienode()); } cur = nodes[cur].child[nxt]; nodes[cur].tm.insert(disc); } } int search(int x, int start, int end) { int cur = 0, ans = 0; for (int i = 30; i >= 0; --i) { int nxt = 1 - (bool) (x & (1 << i)); if (!nodes[cur].child[nxt]) nxt = 1 - nxt; else { auto it = nodes[nodes[cur].child[nxt]].tm.lower_bound(start); if (it == nodes[nodes[cur].child[nxt]].tm.end() || *it > end) nxt = 1 - nxt; } ans |= nxt << i; cur = nodes[cur].child[nxt]; } return ans; } }; struct query { bool tp; int u, v; }; void solve(int tc) { int q; cin >> q; vector<query> qry(q); int n = 2; VVI tree = VVI(2); VI xo(2); loop(i, 0, q) { string s; cin >> s; cin >> qry[i].u >> qry[i].v; if (s == "Add") { qry[i].tp = false; tree[qry[i].u].pb(n); tree.pb({qry[i].u}); xo.pb(xo[qry[i].u] ^ qry[i].v); qry[i].v = n++; } else qry[i].tp = true; } LCA lca(tree); trie tri; for (auto& [tp, u, v] : qry) { if (!tp) { tri.insert(xo[v], lca.vis_start[v]); } else { int x = xo[u]; x ^= xo[lca.get(u, v)]; cout << (x ^ tri.search(x, lca.vis_start[v], lca.vis_end[v])) << nl; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; int x = t; while(t--) solve(x - t); #ifdef LOCAL cerr << "Execution time: " << 1000.f * clock() / CLOCKS_PER_SEC << " ms." << nl; #endif return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...