洛谷4839 - P哥的桶

每个点维护线性基,线段树的两个子节点合并的时候直接把一个点的暴力插入到隔壁即可。

代码:

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
// =================================
// author: memset0
// date: 2019.01.14 16:12:11
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 5e4 + 10;
int n, m;

struct info {
int f[31];
inline void insert(int x) {
for (int i = 30; i >= 0; i--)
if ((x >> i) & 1) {
if (f[i]) x ^= f[i];
else return (void)(f[i] = x);
}
}
inline int calc() {
int ans = 0;
for (int i = 30; i >= 0; i--)
if (!((ans >> i) & 1))
ans ^= f[i];
return ans;
}
};

inline info merge(const info &a, const info &b) {
static info f; f = a;
for (int i = 0; i <= 30; i++)
f.insert(b.f[i]);
return f;
}

struct node {
int l, r, mid;
info x;
} p[N << 2];

void build(int u, int l, int r) {
p[u].l = l, p[u].r = r, p[u].mid = (l + r) >> 1;
if (l == r) return;
build(u << 1, l, p[u].mid);
build(u << 1 | 1, p[u].mid + 1, r);
}

void insert(int u, int k, int x) {
if (p[u].l == p[u].r) return p[u].x.insert(x);
if (k <= p[u].mid) insert(u << 1, k, x);
else insert(u << 1 | 1, k, x);
p[u].x = merge(p[u << 1].x, p[u << 1 | 1].x);
}

info query(int u, int l, int r) {
if (p[u].l == l && p[u].r == r) return p[u].x;
if (r <= p[u].mid) return query(u << 1, l, r);
if (l > p[u].mid) return query(u << 1 | 1, l, r);
return merge(query(u << 1, l, p[u].mid), query(u << 1 | 1, p[u].mid + 1, r));
}

void main() {
read(n), read(m);
build(1, 1, m);
for (int i = 1, k, x, l, r, opt; i <= n; i++) {
read(opt);
if (opt == 1) {
read(k), read(x);
insert(1, k, x);
} else {
read(l), read(r);
print(query(1, l, r).calc(), '\n');
}
}
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×