-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0256.h
More file actions
93 lines (78 loc) · 2.24 KB
/
Problem0256.h
File metadata and controls
93 lines (78 loc) · 2.24 KB
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
//
// Created by Fengwei Zhang on 3/24/22.
//
#ifndef ACWINGSOLUTION_PROBLEM0256_H
#define ACWINGSOLUTION_PROBLEM0256_H
#include <iostream>
using namespace std;
class Problem0256 {
// 对于每个单词,无论是插入、查询,都是从尾部向头部迭代
private:
static const int K = 23;
struct Node {
int max_ver;
Node *kids[2]{};
Node() {
max_ver = -1;
for (auto &item: kids) {
item = nullptr;
}
}
};
void insert(const int &k, const int s[], Node *prev, Node *cur) {
cur->max_ver = k;
for (auto i = K; i >= 0; --i) {
auto j = (s[k] >> i) & 1;
if (prev) {
cur->kids[j ^ 1] = prev->kids[j ^ 1];
prev = prev->kids[j];
}
cur->kids[j] = new Node();
cur->kids[j]->max_ver = k;
cur = cur->kids[j];
}
}
int query(Node *root, const int &st, const int &val, const int s[]) {
for (auto i = K; i >= 0; --i) {
auto j = (val >> i) & 1;
if (root->kids[j ^ 1] && root->kids[j ^ 1]->max_ver >= st) {
root = root->kids[j ^ 1];
} else {
root = root->kids[j];
}
}
return val ^ s[root->max_ver];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
int s[n + m + 1];
Node *root[n + m + 1];
s[0] = 0;
root[0] = new Node();
insert(0, s, nullptr, root[0]);
for (int i = 1, x; i <= n; ++i) {
scanf("%d", &x);
root[i] = new Node();
s[i] = s[i - 1] ^ x;
insert(i, s, root[i - 1], root[i]);
}
while (m--) {
char op[2];
int x, l, r;
scanf("%s", op);
if (op[0] == 'A') {
scanf("%d", &x);
++n;
root[n] = new Node();
s[n] = s[n - 1] ^ x;
insert(n, s, root[n - 1], root[n]);
} else {
scanf("%d%d%d", &l, &r, &x);
printf("%d\n", query(root[r - 1], l - 1, s[n] ^ x, s));
}
}
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0256_H