「SCOI2016」 幸运数字 - 线性基 - 倍增

给你一棵nn个点带点权的树,有mm次查询,每次询问一条链上的最大异或和

n20000,m200000,ai260n\le 20000, m\le 200000, a_i\le 2^{60}

Luogu P3292

Solution

直接倍增+线性基即可

这里主要练一下线性基的合并,直接暴力枚举另一个线性基里的值暴力插入

Code

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
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 2e4 + 100, Maxlen = 60;

int N, Q;
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];
LL W[Maxn];

struct Basis
{

LL A[Maxlen + 1];

inline void init () { memset(A, 0, sizeof A); }

inline int insert (LL x)
{
for (int i = Maxlen; i >= 0; --i)
{
if (!(x & (1ll << i))) continue;
if (!A[i])
{
for (int j = 0; j < i; ++j) if (x & (1ll << j)) x ^= A[j];
for (int j = i + 1; j <= Maxlen; ++j) if (A[j] & (1ll << i)) A[j] ^= x;
A[i] = x;
return 1;
}
x ^= A[i];
}
return 0;
}

friend Basis merge (const Basis &a, const Basis &b)
{
Basis ans = a;
for (int i = 0; i <= Maxlen; ++i) if (b.A[i]) ans.insert (b.A[i]);
return ans;
}

inline LL query (LL ans = 0)
{
for (int i = 0; i <= Maxlen; ++i) Chkmax(ans, ans ^ A[i]);
return ans;
}

} B[15 + 1][Maxn];

inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }

int anc[15 + 1][Maxn], dep[Maxn];

inline void dfs (int x)
{
dep[x] = dep[anc[0][x]] + 1;
B[0][x].insert (W[x]);
for (int i = 1; i <= 15; ++i)
{
anc[i][x] = anc[i - 1][anc[i - 1][x]];
B[i][x] = merge (B[i - 1][x], B[i - 1][anc[i - 1][x]]);
}

for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i]; if (y == anc[0][x]) continue;

anc[0][y] = x;

dfs(y);
}
}

Basis ans;

inline void get_lca (int x, int y)
{
ans.insert(W[x]), ans.insert(W[y]);

if (dep[x] < dep[y]) swap(x, y);
for (int i = 15; i >= 0; --i)
if (dep[anc[i][x]] >= dep[y])
{
ans = merge (ans, B[i][x]);
x = anc[i][x];
}


if (x == y) return ;

for (int i = 15; i >= 0; --i)
if (anc[i][x] != anc[i][y])
{
ans = merge(ans, merge(B[i][x], B[i][y]));
x = anc[i][x], y = anc[i][y];
}

ans.insert(W[x]), ans.insert(W[y]);
ans.insert(W[anc[0][x]]);
}

inline void Solve ()
{
dfs(1);
while (Q--)
{
int x = read<int>(), y = read<int>();
ans.init();
get_lca (x, y);
printf("%lld\n", ans.query());
}
}

inline void Input ()
{
N = read<int>(), Q = read<int>();
for (int i = 1; i <= N; ++i) W[i] = read<LL>();
for (int i = 1; i < N; ++i)
{
int x = read<int>(), y = read<int>();
add_edge (x, y);
add_edge (y, x);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}
感谢你的赞赏!