Submission #475462

#TimeUsernameProblemLanguageResultExecution timeMemory
475462NSGKlasika (COCI20_klasika)C++14
110 / 110
2400 ms417268 KiB
///giangcbg - TST 2022. #include <bits/stdc++.h> #define reset(x, val) memset((x), (val), sizeof (x)) #define bit(X, i) (((X) >> (i)) & 1) #define all(X) (X).begin(), (X).end() #define on(X,i) (X|(1<<i)) #define off(X,i) (X^(1<<i)) #define cntbit(X) __builtin_popcountll(X) #define pb push_back #define ep emplace_back #define fi first #define se second using namespace std; const char ginp[]="KLASIKA.INP"; const char gout[]="KLASIKA.OUT"; void debug_out() { cerr << '\n'; } template <typename Head, typename ...Tail> void debug_out(Head H, Tail ...T) { cerr << " " << H; debug_out(T...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) void read() {} template <typename Head, typename ...Tail> void read(Head &H, Tail &...T) { register char c; bool sign = 0; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') sign = !sign; H = c - '0'; for (c = getchar(); isdigit(c); c = getchar()) H = H * 10 + c - '0'; if (sign) H = -H; read(T...); } typedef long long ll; typedef pair <int, int> ii; typedef pair <int, ii> iii; typedef vector <int> vi; typedef vector <ii> vii; mt19937 rd(chrono :: steady_clock :: now().time_since_epoch().count()); #define rand rd ll Rand(ll l, ll h) { return l + rand() % (h - l + 1); } const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const int N = 2e5 + 5; const int oo = 1e9 + 7; struct query { int t, a, b; }; struct Trie { Trie *c[2]; set <int> s; Trie() { c[0] = c[1] = nullptr; s.clear(); } }; Trie *root; int m,n; query q[N]; vi g[N]; int in[N], out[N], f[N]; void enter() { cin >> m; n = 1; f[1] = 0; ///sumXor for (int i = 1; i <= m; i++) { string s; cin >> s >> q[i].a >> q[i].b; //debug(q[i].s, q[i].a, q[i].b); if (s == "Add") { q[i].t = 1; ++n; g[q[i].a].pb(n); f[n] = f[q[i].a] ^ q[i].b; q[i].a = n; q[i].b = f[n]; } else q[i].t = 2; } } void dfs(int u) { in[u] = ++in[0]; for (int v : g[u]) dfs(v); out[u] = in[0]; } inline void Add(int x, int time) { Trie *p = root; for (int i = 29; i >= 0; i--) { int id = bit(x,i); if (p -> c[id] == nullptr) p -> c[id] = new Trie(); p = p -> c[id]; p -> s.insert(time); } } inline bool check(Trie *p, int b) { auto it = p->s.lower_bound(in[b]); if (it == p->s.end() || *it > out[b]) return 0; return 1; } inline int Find(int a, int b) { Trie *p = root; int res = 0; for (int i = 29; i >= 0; i--) { int id = bit(f[a],i); if (p -> c[!id] != nullptr && check(p -> c[!id], b)) { res |= (1<<i); p = p -> c[!id]; } else p = p -> c[id]; } return res; } void solve() { dfs(1); root = new Trie(); Add(f[1], in[1]); for (int i = 1; i <= m; i++) { if (q[i].t == 2) cout << Find(q[i].a, q[i].b) << '\n'; else Add(q[i].b, in[q[i].a]); } } int main() { ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE //freopen(ginp, "r", stdin); //freopen(gout, "w", stdout); #endif // ONLINE_JUDGE enter(); solve(); cerr << "\nTime : " << clock() << "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...