Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4735 最大异或和

P4735 最大异或和

异或前缀和,建可持久化Trie,和主席树类似。

查看代码
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
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
const int N = 6e5 + 10, M = 24;
struct Node { int nxt[2], v; } tr[N * M];
int n, m, idx, rt[N], w[N];
void insert (int &x, int k, int t = M - 1)
{
tr[++idx] = tr[x]; x = idx;
++tr[x].v;
if (!~t) return;
insert(tr[x].nxt[k >> t & 1], k, t - 1);
}
int query (int x, int y, int k, int t = M - 1)
{
if (!~t) return 0;
int c = k >> t & 1;
if (tr[tr[y].nxt[c ^ 1]].v > tr[tr[x].nxt[c ^ 1]].v)
return query(tr[x].nxt[c ^ 1], tr[y].nxt[c ^ 1], k, t - 1) | 1 << t;
return query(tr[x].nxt[c], tr[y].nxt[c], k, t - 1);
}
int main ()
{
read(n, m);
insert(rt[1], 0);
for (int i = 1; i <= n; ++i)
read(w[i]), insert(rt[i + 1] = rt[i], w[i] ^= w[i - 1]);
for (char op[3]; m; --m)
{
scanf("%s", op);
if (*op == 'A')
{
int x; read(x);
w[++n] = x, w[n] ^= w[n - 1];
insert(rt[n + 1] = rt[n], w[n]);
}
else if (*op == 'Q')
{
int l, r, x; read(l, r, x);
write(query(rt[l - 1], rt[r], x ^ w[n])), puts("");
}
}
return 0;
}