-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistent Trie.cpp
193 lines (162 loc) · 3.64 KB
/
Persistent Trie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Solution of a Codechef problem - XRQRS
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using llint = long long;
using ullint = unsigned long long;
using pii = pair <int,int>;
using pcc = pair <char,char>;
using pss = pair <string,string>;
using vi = vector<int>;
using vii = vi::iterator;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define all(c) c.begin(),c.end()
#define ms(name,val) memset(name, val, sizeof name)
#define np nullptr
const int M = 5e5+5;
struct Node
{
int cnt = 0;
Node *c[2] = {np,np};
};
vector <Node*> nodes;
void delTrie()
{
for (int t1 = 0; t1 < nodes.size(); ++t1)
delete nodes[t1];
}
int len;
Node *root[M];
Node* Insert(Node *old, int &x)
{
Node *n = new Node;
nodes.push_back(n);
n->cnt = old->cnt+1;
Node *temp = n;
for (int lvl = 19; lvl > -1; --lvl)
{
bool b = x&(1<<lvl);
temp->c[b^1] = old->c[b^1];
temp->c[b] = new Node;
nodes.push_back(temp->c[b]);
temp = temp->c[b];
old = old->c[b];
temp->cnt = old->cnt+1;
}
return n;
}
int XOR_max(int &L, int &R, int &x)
{
// Find a number 'ans' in range [L,R] such
// that XOR of 'x' and 'ans' is max
Node *rootL = root[L-1];
Node *rootR = root[R];
int ans = 0;
for (int lvl = 19; lvl > -1; --lvl)
{
bool b = x&(1<<lvl);
bool hasLeft = (rootL->c[0] != rootR->c[0]);
bool hasRight = (rootL->c[1] != rootR->c[1]);
if ((b && hasLeft) || (!b && hasRight))
{
if (!b) ans |= (1<<lvl);
rootL = rootL->c[b^1];
rootR = rootR->c[b^1];
}
else
{
if (b) ans |= (1<<lvl);
rootL = rootL->c[b];
rootR = rootR->c[b];
}
}
return ans;
}
int LessThanOrEqualTo(int &L, int &R, int &x)
{
Node *rootL = root[L-1];
Node *rootR = root[R];
int ans = 0;
for (int lvl = 19; lvl > -1; --lvl)
{
bool b = x&(1<<lvl);
if (b) ans += (rootR->c[0]->cnt - rootL->c[0]->cnt);
rootL = rootL->c[b];
rootR = rootR->c[b];
}
return ans + (rootR->cnt - rootL->cnt);
}
int Kth(int &L, int &R, int &k)
{
Node *rootL = root[L-1];
Node *rootR = root[R];
int ans = 0;
for (int lvl = 19; lvl > -1; --lvl)
{
int cntLeft = rootR->c[0]->cnt - rootL->c[0]->cnt;
if (k <= cntLeft)
{
rootL = rootL->c[0];
rootR = rootR->c[0];
}
else
{
k -= cntLeft;
ans |= (1<<lvl);
rootL = rootL->c[1];
rootR = rootR->c[1];
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
int q;
cin >> q;
len = 1;
root[0] = new Node;
root[0]->c[0] = root[0]->c[1] = root[0];
while (q)
{
--q;
int type;
cin >> type;
if (!type)
{
int x;
cin >> x;
root[len] = Insert(root[len-1], x);
++len;
}
else if (type == 1)
{
int L, R, x;
cin >> L >> R >> x;
cout << XOR_max(L,R,x) << '\n';
}
else if (type == 2)
{
int k;
cin >> k;
len -= k;
}
else if (type == 3)
{
int L, R, x;
cin >> L >> R >> x;
cout << LessThanOrEqualTo(L,R,x) << '\n';
}
else
{
int L, R, k;
cin >> L >> R >> k;
cout << Kth(L,R,k) << '\n';
}
}
delTrie(), delete root[0];
return 0;
}