#include <bits/stdc++.h>
using namespace std;
//#define int int64_t //be careful about this
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)
#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a, b, sizeof(a))
template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x;}
template<class H, class... T> void rd(H& h, T&... t) { rd(h); rd(t...);}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a);}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0;}
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0;}
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// remove this when size of input is not fixed.
cin.exceptions(cin.failbit);
cout.precision(15); cout << fixed;
if(SZ(s)){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
/*********************************************Look Below******************************************************************/
const int MAX_N = 2e5 + 7;
vector<int> g[MAX_N], st(MAX_N), en(MAX_N), val(MAX_N);
int timer;
void dfs(int i){
st[i] = timer++ ;
for(auto j : g[i]) dfs(j);
en[i] = timer - 1 ;
}
const int LOG = 30;
template<const int ALPHABET>
struct prefix_tree {
struct trie_node {
int link[ALPHABET];
set<int> whose;
trie_node() {
memset(link, -1, sizeof(link));
}
};
vector<trie_node> trie;
prefix_tree(){
trie.emplace_back();
}
void create_node(int parent, int c) {
trie.emplace_back();
}
int get_or_create_node(int current, int c) {
if (trie[current].link[c] < 0) {
trie[current].link[c] = trie.size();
create_node(current, c);
}
return trie[current].link[c];
}
void insert_str(const int & x,const int & i) {
int current = 0;
for(int j = LOG - 1; ~j; --j){
int c;
if((x >> j) & 1){
c = 1;
}else{
c = 0;
}
current = get_or_create_node(current,c);
trie[current].whose.insert(i);
}
return;
}
// checks whether in trie[x].whose has some element b/w l,r;
bool is(const int & x,const int & l,const int & r){
auto itr = trie[x].whose.lower_bound(l);
return itr != trie[x].whose.end() && (*itr) <= r;
}
int query(const int & x,const int & l,const int & r){
int current = 0;
int ans = 0;
for(int j = LOG - 1; ~j; --j){
int c;
if((x >> j) & 1){
c = 1;
}else{
c = 0;
}
int need = trie[current].link[!c];
if(need > 0 && is(need,l,r)){
ans ^= (1 << j);
current = need;
}else{
current = trie[current].link[c];
assert(is(current,l,r));
}
}
return ans;
}
};
prefix_tree<2> trie;
signed main(){
setIO();
int q; rd(q);
vt<ar<int,3>> queries;
val[1] = 0;
int N = 1;
while(q--){
string s;
rd(s);
if(s == "Add"){
int x,y;
rd(x,y);
g[x].pb(++N);
val[N] = (val[x]^y);
queries.pb({0,N,-1});
}else if(s == "Query"){
int a,b; rd(a,b);
queries.pb({1,a,b});
}else{
assert(false);
}
}
dfs(1);
for(auto [t,a,b] : queries){
if(t){
cout << trie.query(val[a],st[b],en[b]) << endl;
}else{
trie.insert_str(val[a],st[a]);
}
}
}
Compilation message
klasika.cpp: In function 'void setIO(std::string)':
klasika.cpp:82:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
klasika.cpp:83:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
14764 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
14764 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
88 ms |
24536 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
14764 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |