This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 3e5 + 10;
int n;
struct Query {
int a, t;
Query(int a = 0, int t = 0) : a(a), t(t) {}
};
vector<Query> Q[N];
struct trie {
int maxBit = 30;
int it;
vector<vector<int>> nwt;
vector<int> t;
set<int> S;
map<int, int> tick;
void add(int val, int time) {
S.insert(val);
if (!tick[val] || tick[val] > time) tick[val] = time;
int g = 0;
for (int j = maxBit; j >= 1; --j) {
int bit = (val >> j - 1 & 1);
while (nwt.size() <= g) nwt.push_back(vector<int>(2));
if (!nwt[g][bit]) nwt[g][bit] = ++it;
g = nwt[g][bit];
while (t.size() <= it) t.push_back(N);
t[g] = min(t[g], time);
}
}
int get(int val, int time) {
int g = 0;
int ret = 0;
for (int j = maxBit; j >= 1; --j) {
while (nwt.size() <= g) nwt.push_back(vector<int>(2));
while (t.size() <= it) t.push_back(N);
int bit = (val >> j - 1 & 1);
if (nwt[g][!bit] && t[nwt[g][!bit]] <= time) {
ret |= (1 << j - 1);
g = nwt[g][!bit];
} else g = nwt[g][bit];
}
return ret;
}
vector<pii> allValues() {
vector<pii> ret;
for (auto &i : S) ret.push_back({i, tick[i]});
return ret;
}
void clear() {
nwt.clear();
t.clear();
tick.clear();
S.clear();
}
} node[N];
vector<pii> ad[N];
int d[N], t[N];
int answer[N];
void dfs(int u, int pre = 0) {
int best = 0;
for (const auto& duck : ad[u]) {
int v, w; tie(v, w) = duck;
if (v == pre) continue;
dfs(v, u);
if (!best || node[v].nwt.size() > node[best].nwt.size()) best = v;
}
swap(node[u], node[best]);
for (const auto& duck : ad[u]) {
int v, w; tie(v, w) = duck;
if (v == pre || v == best) continue;
for (auto &i : node[v].allValues()) node[u].add(i.first, i.second);
node[v].clear();
}
node[u].add(d[u], t[u]);
for (const auto& duck : Q[u]) {
int a = duck.a, time = duck.t;
answer[time] = node[u].get(d[a], time);
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("klasika.in.3g", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
cin >> n;
int cnt = 1;
for (int i = 1; i <= n; ++i) {
string ty;
int x, y;
cin >> ty >> x >> y;
if (ty == "Add") {
cnt += 1;
ad[x].push_back({cnt, y});
ad[cnt].push_back({x, y});
d[cnt] = (d[x] ^ y);
t[cnt] = i;
} else {
Q[y].push_back(Query(x, i));
}
}
memset(answer, -1, sizeof answer);
dfs(1);
for (int i = 1; i <= n; ++i)
if (answer[i] != -1) cout << answer[i] << "\n";
// cerr << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
}
Compilation message (stderr)
klasika.cpp: In member function 'void trie::add(int, int)':
klasika.cpp:28:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
28 | int bit = (val >> j - 1 & 1);
| ~~^~~
klasika.cpp:30:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
30 | while (nwt.size() <= g) nwt.push_back(vector<int>(2));
| ~~~~~~~~~~~^~~~
klasika.cpp:35:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
35 | while (t.size() <= it) t.push_back(N);
| ~~~~~~~~~^~~~~
klasika.cpp: In member function 'int trie::get(int, int)':
klasika.cpp:43:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
43 | while (nwt.size() <= g) nwt.push_back(vector<int>(2));
| ~~~~~~~~~~~^~~~
klasika.cpp:44:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | while (t.size() <= it) t.push_back(N);
| ~~~~~~~~~^~~~~
klasika.cpp:45:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
45 | int bit = (val >> j - 1 & 1);
| ~~^~~
klasika.cpp:48:24: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
48 | ret |= (1 << j - 1);
| ~~^~~
klasika.cpp: In function 'int32_t main()':
klasika.cpp:102:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen("duck.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
klasika.cpp:103:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen("duck.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |