Submission #770109

#TimeUsernameProblemLanguageResultExecution timeMemory
770109vjudge1Klasika (COCI20_klasika)C++17
33 / 110
2304 ms524288 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 { public: vector<vector<int>> trie; vector<set<int>> st; Trie() { trie.push_back(vector<int> (2, -1)); st.push_back(set<int> ()); }; void add(int x, int ins) { int t = 0; for (int i = lg - 1; i >= 0; i--) { int bit = (x >> i) & 1; if (trie[t][bit] == -1) { trie[t][bit] = (int) trie.size(); trie.emplace_back(2, -1); st.push_back(set<int> ()); st.push_back(set<int> ()); } t = trie[t][bit]; st[t].insert(ins); } } int get(int x, int low, int high) { int t = 0; int ret = 0; auto isBad = [&](int bit) { if (trie[t][bit] == -1) { return true; } if (st[trie[t][bit]].empty()) { return true; } auto ptr = st[trie[t][bit]].lower_bound(low); if (ptr != st[trie[t][bit]].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 = trie[t][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); Trie T; T.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]; T.add(pref[u], in[u]); } else { int a = queries[i][1]; int b = queries[i][2]; cout << T.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...