Submission #770114

#TimeUsernameProblemLanguageResultExecution timeMemory
770114vjudge1Klasika (COCI20_klasika)C++17
110 / 110
2131 ms435044 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "C:\GCC\debug.h" #else #define debug(...) void(42) #endif const int lg = 31; struct Trie { Trie* nxt[2]; set<int> st; Trie() { nxt[0] = nullptr; nxt[1] = nullptr; } }; Trie *root; void add(int x, int ins) { Trie *t = root; for (int i = lg - 1; i >= 0; i--) { int bit = (x >> i) & 1; if (t->nxt[bit] == nullptr) { t->nxt[bit] = new Trie(); } t = t->nxt[bit]; t->st.insert(ins); } } int get(int x, int low, int high) { Trie *t = root; int ret = 0; auto isBad = [&](int bit) { if (t->nxt[bit] == nullptr) { return true; } Trie *p = t; p = t->nxt[bit]; if (p->st.empty()) { return true; } auto ptr = p->st.lower_bound(low); if (ptr != p->st.end() && *ptr <= high) { return false; } return true; }; for (int i = lg - 1; i >= 0; i--) { int bit = (x >> i) & 1; bit ^= 1; if (isBad(bit)) { bit ^= 1; } else { ret |= (1 << i); } if (isBad(bit)) { return 0; } t = t->nxt[bit]; } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q; int n = q + 1; vector<vector<int>> adj(n); vector<int> pref(n); auto Add = [&](int x, int y, int w) { adj[x].push_back(y); pref[y] = pref[x] ^ w; }; int cnt = 1; vector<array<int, 3>> queries; for (int i = 0; i < q; i++) { string type; cin >> type; if (type == "Add") { int x, w; cin >> x >> w; --x; Add(x, cnt, w); queries.push_back({1, cnt, 0}); ++cnt; } else { int a, b; cin >> a >> b; --a, --b; queries.push_back({2, a, b}); } } vector<int> in(n); vector<int> out(n); vector<int> euler_tour; function<void(int)> Dfs = [&](int u) { in[u] = (int) euler_tour.size(); euler_tour.push_back(u); for (auto v : adj[u]) { Dfs(v); } out[u] = (int) euler_tour.size() - 1; }; Dfs(0); root = new Trie(); add(0, 0); for (int i = 0; i < (int) queries.size(); i++) { int type = queries[i][0]; if (type == 1) { int u = queries[i][1]; add(pref[u], in[u]); } else { int a = queries[i][1]; int b = queries[i][2]; cout << get(pref[a], in[b], out[b]) << '\n'; } } 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...