Submission #1119875

#TimeUsernameProblemLanguageResultExecution timeMemory
1119875steveonalexKlasika (COCI20_klasika)C++17
110 / 110
2113 ms271132 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return __lg(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n"){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct Trie{ struct Node{ int child[2]; Node(){memset(child, -1, sizeof child);} }; int n; vector<Node> a; Trie(){ a.push_back(Node()); } void add_child(int &x){ x = a.size(); a.push_back(Node()); } void add(int x){ int id = 0; for(int bit = 29; bit >= 0; --bit){ int digit = GETBIT(x, bit); if (a[id].child[digit] == -1) add_child(a[id].child[digit]); id = a[id].child[digit]; } } int get_max_xor(int x){ if (a[0].child[0] == -1 && a[0].child[1] == -1) return 0; int id = 0, ans = 0; for(int bit = 29; bit >= 0; --bit){ int digit = GETBIT(x, bit); if (a[id].child[1 - digit] != -1){ id = a[id].child[1 - digit]; ans += MASK(bit); } else id = a[id].child[digit]; } return ans; } }; struct Trie2D{ struct Node{ int child[8]; Trie tri; Node(){memset(child, -1, sizeof child);} }; int n; vector<Node> a; Trie2D(){ a.push_back(Node()); } void add_child(int &x){ x = a.size(); a.push_back(Node()); } void add(int x, int y){ int id = 0; for(int bit = 5; bit >= 0; --bit){ a[id].tri.add(y); int digit = GETBIT(x, bit * 3 + 2) * 4 + GETBIT(x, bit * 3 + 1) * 2 + GETBIT(x, bit * 3); if (a[id].child[digit] == -1) add_child(a[id].child[digit]); id = a[id].child[digit]; } a[id].tri.add(y); } int query(int l, int r, int x, int layer, int id){ if (l == 0 && r == MASK(layer + 3) - 1){ return a[id].tri.get_max_xor(x); } int ans = 0; for(int i = 0; i < 8; ++i) if (a[id].child[i] != -1){ int _l = l - MASK(layer) * i, _r = r - MASK(layer) * i; maximize(_l, 0); minimize(_r, MASK(layer) - 1); if (_l > _r) continue; maximize(ans, query(_l, _r, x, layer - 3, a[id].child[i])); } return ans; } int query(int l, int r, int x){ return query(l, r, x, 15, 0); } }; const int N = 2e5 + 69; int n; vector<pair<int, int>> graph[N]; int h[N], in[N], out[N], dfs_cnt = 0; void dfs(int u, int p){ in[u] = out[u] = ++dfs_cnt; for(pair<int, int> v: graph[u]) if (v.first != p){ h[v.first] = h[u] ^ v.second; dfs(v.first, u); out[u] = out[v.first]; } } void solve(){ int q; cin >> q; vector<array<int, 3>> query(q); n = 1; for(int i = 0; i<q; ++i){ string type; int u, v; cin >> type >> u >> v; if (type == "Add") { n++; query[i] = {{1, n, v}}; graph[u].push_back({n, v}); graph[n].push_back({u, v}); } else query[i] = {{2, u, v}}; } dfs(1, 0); Trie2D tri; tri.add(1, 0); for(auto i: query){ if (i[0] == 1){ int x = h[i[1]], u = in[i[1]]; tri.add(u, x); } else{ int x = i[1], y = i[2]; x = h[x]; int l = in[y], r = out[y]; int ans = tri.query(l, r, x); cout << ans << "\n"; } } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\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...