/*** Coding for Fun ^^ وَمَا تَوْفِيقِي إِلَّا بِاللَّهِ ۚ عَلَيْهِ تَوَكَّلْتُ وَإِلَيْهِ أُنِيبُ ***/
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned ll
#define ld long double
#define ed '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fixed(x) fixed<<setprecision(x)
#define memo(dp,x) memset(dp,x,sizeof(dp))
#define sumOf(a) (ll)((-1 + sqrt(8LL * a + 1)) / 2)
#define debug cerr
#define Good_Bay return
using namespace std;
using namespace __gnu_pbds;
std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
template < typename T > using ordered_set = tree< T, null_type, less_equal< T >, rb_tree_tag, tree_order_statistics_node_update >;
template < typename T > istream& operator >> (istream& in, vector < T >& v) {
for (auto& x : v) in >> x;
return in;
}
template < typename T > ostream& operator << (ostream& out, const vector < T >& v) {
for (const T& x : v) out << x << ' ';
return out;
}
void Accept() { // for VS code
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #else
// freopen("darwin.in", "r", stdin);
// freopen("visitfj.out", "w", stdout);
// #endif
}
constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
const double pi = acos(-1), EPS = 1e-7;
constexpr ll OO = 0x3f3f3f3f3f3f3f3f;
constexpr int dy[] = { 0 , 1 , -1 , 0 , 1 , -1 , 1 , -1 };
constexpr int dx[] = { 1 , 0 , 0 , -1 , 1 , -1 , -1 , 1 };
struct query {
int t, a, b;
query(int t, int a, int b) : t(t), a(a), b(b) {}
};
vector<int>adj[N];
vector<int>in(N), out(N);
vector<query>Q;
int n = 1, id = 1;
void dfs(int u) {
in[u] = id++;
for (auto& v : adj[u]) dfs(v);
out[u] = id++;
}
struct binary_trie {
vector<array<int, 2>> trie;
vector<array<set<int>, 2>> numsIn;
vector<array<set<int>, 2>> numsOut;
int LOG;
binary_trie(int _LOG) : LOG(_LOG) {
trie = { array<int, 2>{-1, -1} };
numsIn = { array<set<int>, 2>{} };
numsOut = { array<set<int>, 2>{} };
}
void insert(int x, int u) {
int v = 0;
for (int bit = LOG - 1; bit >= 0; --bit) {
int to = ((1 << bit) & x ? 1 : 0);
if (~trie[v][to]) {
numsIn[v][to].insert(in[u]);
numsOut[v][to].insert(out[u]);
v = trie[v][to];
}
else {
numsIn[v][to].insert(in[u]);
numsOut[v][to].insert(out[u]);
v = (trie[v][to] = (int)trie.size());
trie.push_back(array<int, 2>{-1, -1});
numsIn.push_back(array<set<int>, 2>{});
numsOut.push_back(array<set<int>, 2>{});
}
}
}
int query_max(int x, int u) {
int v = 0, sum = 0;
for (int bit = LOG - 1; bit >= 0; bit--) {
int to = ((1 << bit) & x ? 0 : 1);
auto itIn = numsIn[v][to].lower_bound(in[u]);
auto itOut = numsOut[v][to].lower_bound(out[u]);
if (~trie[v][to] and *itIn >= in[u] and *(--itOut) <= out[u]) {
v = trie[v][to], sum += to * (1 << bit);
}
else {
v = trie[v][1 - to], sum += (1 - to) * (1 << bit);
}
}
return x ^ sum;
}
};
void doWork(int T) {
int q;
cin >> q;
binary_trie tr(32);
for (int i = 0;i < q;i++) {
string s;
int a, b;
cin >> s >> a >> b;
if (s == "Add") {
adj[a].push_back(++n);
Q.push_back({ 1,a,b });
}
else {
Q.push_back({ 2,a,b });
}
}
dfs(1);
vector<int>preXor(n);
preXor[1] = 0;
n = 1;
tr.insert(0, 1);
for (auto i : Q) {
if (i.t == 2) cout << tr.query_max(preXor[i.a], i.b) << ed;
else {
preXor[++n] = preXor[i.a] ^ i.b;
tr.insert(preXor[n], n);
}
}
}
int main() {
// debug << "M3L4 El-Code MYTFHM4";
Accept();
int _ = 1;
// cin >> _;
for (int __ = 1;__ <= _;__++) {
// cout << "Case " << __ << ": ";
doWork(__);
if (__ < _)cout << '\n';
// cout << '\n';
}
Good_Bay 0;
}/*** Coding for Fun ^^ وَمَا تَوْفِيقِي إِلَّا بِاللَّهِ ۚ عَلَيْهِ تَوَكَّلْتُ وَإِلَيْهِ أُنِيبُ ***/
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned ll
#define ld long double
#define ed '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fixed(x) fixed<<setprecision(x)
#define memo(dp,x) memset(dp,x,sizeof(dp))
#define sumOf(a) (ll)((-1 + sqrt(8LL * a + 1)) / 2)
#define debug cerr
#define Good_Bay return
using namespace std;
using namespace __gnu_pbds;
std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
template < typename T > using ordered_set = tree< T, null_type, less_equal< T >, rb_tree_tag, tree_order_statistics_node_update >;
template < typename T > istream& operator >> (istream& in, vector < T >& v) {
for (auto& x : v) in >> x;
return in;
}
template < typename T > ostream& operator << (ostream& out, const vector < T >& v) {
for (const T& x : v) out << x << ' ';
return out;
}
void Accept() { // for VS code
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #else
// freopen("darwin.in", "r", stdin);
// freopen("visitfj.out", "w", stdout);
// #endif
}
constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
const double pi = acos(-1), EPS = 1e-7;
constexpr ll OO = 0x3f3f3f3f3f3f3f3f;
constexpr int dy[] = { 0 , 1 , -1 , 0 , 1 , -1 , 1 , -1 };
constexpr int dx[] = { 1 , 0 , 0 , -1 , 1 , -1 , -1 , 1 };
struct query {
int t, a, b;
query(int t, int a, int b) : t(t), a(a), b(b) {}
};
vector<int>adj[N];
vector<int>in(N), out(N);
vector<query>Q;
int n = 1, id = 1;
void dfs(int u) {
in[u] = id++;
for (auto& v : adj[u]) dfs(v);
out[u] = id++;
}
struct binary_trie {
vector<array<int, 2>> trie;
vector<array<set<int>, 2>> numsIn;
vector<array<set<int>, 2>> numsOut;
int LOG;
binary_trie(int _LOG) : LOG(_LOG) {
trie = { array<int, 2>{-1, -1} };
numsIn = { array<set<int>, 2>{} };
numsOut = { array<set<int>, 2>{} };
}
void insert(int x, int u) {
int v = 0;
for (int bit = LOG - 1; bit >= 0; --bit) {
int to = ((1 << bit) & x ? 1 : 0);
if (~trie[v][to]) {
numsIn[v][to].insert(in[u]);
numsOut[v][to].insert(out[u]);
v = trie[v][to];
}
else {
numsIn[v][to].insert(in[u]);
numsOut[v][to].insert(out[u]);
v = (trie[v][to] = (int)trie.size());
trie.push_back(array<int, 2>{-1, -1});
numsIn.push_back(array<set<int>, 2>{});
numsOut.push_back(array<set<int>, 2>{});
}
}
}
int query_max(int x, int u) {
int v = 0, sum = 0;
for (int bit = LOG - 1; bit >= 0; bit--) {
int to = ((1 << bit) & x ? 0 : 1);
auto itIn = numsIn[v][to].lower_bound(in[u]);
auto itOut = numsOut[v][to].lower_bound(out[u]);
if (~trie[v][to] and *itIn >= in[u] and *(--itOut) <= out[u]) {
v = trie[v][to], sum += to * (1 << bit);
}
else {
v = trie[v][1 - to], sum += (1 - to) * (1 << bit);
}
}
return x ^ sum;
}
};
void doWork(int T) {
int q;
cin >> q;
binary_trie tr(32);
for (int i = 0;i < q;i++) {
string s;
int a, b;
cin >> s >> a >> b;
if (s == "Add") {
adj[a].push_back(++n);
Q.push_back({ 1,a,b });
}
else {
Q.push_back({ 2,a,b });
}
}
dfs(1);
vector<int>preXor(n);
preXor[1] = 0;
n = 1;
tr.insert(0, 1);
for (auto i : Q) {
if (i.t == 2) cout << tr.query_max(preXor[i.a], i.b) << ed;
else {
preXor[++n] = preXor[i.a] ^ i.b;
tr.insert(preXor[n], n);
}
}
}
int main() {
// debug << "M3L4 El-Code MYTFHM4";
Accept();
int _ = 1;
// cin >> _;
for (int __ = 1;__ <= _;__++) {
// cout << "Case " << __ << ": ";
doWork(__);
if (__ < _)cout << '\n';
// cout << '\n';
}
Good_Bay 0;
}
Compilation message
klasika.cpp:185:17: error: redefinition of 'std::mt19937_64 rng'
185 | std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
| ^~~
klasika.cpp:22:17: note: 'std::mt19937_64 rng' previously declared here
22 | std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
| ^~~
klasika.cpp:189:34: error: redefinition of 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)'
189 | template < typename T > istream& operator >> (istream& in, vector < T >& v) {
| ^~~~~~~~
klasika.cpp:26:34: note: 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)' previously declared here
26 | template < typename T > istream& operator >> (istream& in, vector < T >& v) {
| ^~~~~~~~
klasika.cpp:194:34: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)'
194 | template < typename T > ostream& operator << (ostream& out, const vector < T >& v) {
| ^~~~~~~~
klasika.cpp:31:34: note: 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)' previously declared here
31 | template < typename T > ostream& operator << (ostream& out, const vector < T >& v) {
| ^~~~~~~~
klasika.cpp:200:6: error: redefinition of 'void Accept()'
200 | void Accept() { // for VS code
| ^~~~~~
klasika.cpp:37:6: note: 'void Accept()' previously defined here
37 | void Accept() { // for VS code
| ^~~~~~
klasika.cpp:213:15: error: redefinition of 'constexpr const int mod'
213 | constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
| ^~~
klasika.cpp:50:15: note: 'constexpr const int mod' previously defined here
50 | constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
| ^~~
klasika.cpp:213:30: error: redefinition of 'constexpr const int oo'
213 | constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
| ^~
klasika.cpp:50:30: note: 'constexpr const int oo' previously defined here
50 | constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
| ^~
klasika.cpp:213:47: error: redefinition of 'constexpr const int N'
213 | constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
| ^
klasika.cpp:50:47: note: 'constexpr const int N' previously defined here
50 | constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 2e5 + 5;
| ^
klasika.cpp:214:14: error: redefinition of 'const double pi'
214 | const double pi = acos(-1), EPS = 1e-7;
| ^~
klasika.cpp:51:14: note: 'const double pi' previously defined here
51 | const double pi = acos(-1), EPS = 1e-7;
| ^~
klasika.cpp:214:29: error: redefinition of 'const double EPS'
214 | const double pi = acos(-1), EPS = 1e-7;
| ^~~
klasika.cpp:51:29: note: 'const double EPS' previously defined here
51 | const double pi = acos(-1), EPS = 1e-7;
| ^~~
klasika.cpp:215:14: error: redefinition of 'constexpr const long long int OO'
215 | constexpr ll OO = 0x3f3f3f3f3f3f3f3f;
| ^~
klasika.cpp:52:14: note: 'constexpr const long long int OO' previously defined here
52 | constexpr ll OO = 0x3f3f3f3f3f3f3f3f;
| ^~
klasika.cpp:217:15: error: redefinition of 'constexpr const int dy []'
217 | constexpr int dy[] = { 0 , 1 , -1 , 0 , 1 , -1 , 1 , -1 };
| ^~
klasika.cpp:54:15: note: 'constexpr const int dy [8]' previously defined here
54 | constexpr int dy[] = { 0 , 1 , -1 , 0 , 1 , -1 , 1 , -1 };
| ^~
klasika.cpp:218:15: error: redefinition of 'constexpr const int dx []'
218 | constexpr int dx[] = { 1 , 0 , 0 , -1 , 1 , -1 , -1 , 1 };
| ^~
klasika.cpp:55:15: note: 'constexpr const int dx [8]' previously defined here
55 | constexpr int dx[] = { 1 , 0 , 0 , -1 , 1 , -1 , -1 , 1 };
| ^~
klasika.cpp:220:8: error: redefinition of 'struct query'
220 | struct query {
| ^~~~~
klasika.cpp:57:8: note: previous definition of 'struct query'
57 | struct query {
| ^~~~~
klasika.cpp:225:12: error: redefinition of 'std::vector<int> adj [200005]'
225 | vector<int>adj[N];
| ^~~
klasika.cpp:62:12: note: 'std::vector<int> adj [200005]' previously declared here
62 | vector<int>adj[N];
| ^~~
klasika.cpp:226:12: error: redefinition of 'std::vector<int> in'
226 | vector<int>in(N), out(N);
| ^~
klasika.cpp:63:12: note: 'std::vector<int> in' previously declared here
63 | vector<int>in(N), out(N);
| ^~
klasika.cpp:226:19: error: redefinition of 'std::vector<int> out'
226 | vector<int>in(N), out(N);
| ^~~
klasika.cpp:63:19: note: 'std::vector<int> out' previously declared here
63 | vector<int>in(N), out(N);
| ^~~
klasika.cpp:227:14: error: redefinition of 'std::vector<query> Q'
227 | vector<query>Q;
| ^
klasika.cpp:64:14: note: 'std::vector<query> Q' previously declared here
64 | vector<query>Q;
| ^
klasika.cpp:228:5: error: redefinition of 'int n'
228 | int n = 1, id = 1;
| ^
klasika.cpp:65:5: note: 'int n' previously defined here
65 | int n = 1, id = 1;
| ^
klasika.cpp:228:12: error: redefinition of 'int id'
228 | int n = 1, id = 1;
| ^~
klasika.cpp:65:12: note: 'int id' previously defined here
65 | int n = 1, id = 1;
| ^~
klasika.cpp:230:6: error: redefinition of 'void dfs(int)'
230 | void dfs(int u) {
| ^~~
klasika.cpp:67:6: note: 'void dfs(int)' previously defined here
67 | void dfs(int u) {
| ^~~
klasika.cpp:236:8: error: redefinition of 'struct binary_trie'
236 | struct binary_trie {
| ^~~~~~~~~~~
klasika.cpp:73:8: note: previous definition of 'struct binary_trie'
73 | struct binary_trie {
| ^~~~~~~~~~~
klasika.cpp:285:6: error: redefinition of 'void doWork(int)'
285 | void doWork(int T) {
| ^~~~~~
klasika.cpp:122:6: note: 'void doWork(int)' previously defined here
122 | void doWork(int T) {
| ^~~~~~
klasika.cpp:315:5: error: redefinition of 'int main()'
315 | int main() {
| ^~~~
klasika.cpp:152:5: note: 'int main()' previously defined here
152 | int main() {
| ^~~~