[comet64D]系统设计

Description


$$
1 \le n, m, q \le 5 \times 10 ^5
$$
根不一定为 1。

Solution

官方题解

暴力就是按题意模拟。

仔细考虑这个模拟的过程。

…字符串匹配?

具体来说,把树上每个节点到根所经过的边的序号(边以编号大小排序)抽象成一个字符串,记作 $s _i$,将 $a _{l, \dots, r}$ 也抽象为字符串,那么相当于是找到一个最大的 $p$,满足 $\exists s _j = s _i + a _{l, \dots, p}$,$j$ 即为要输出的点。

可以配合官方题解的图看看。

因为 $p$ 显然具有单调性,所以如果没有修改,就可以直接预处理序列 $a$ 的前缀哈希值和树上节点的哈希值,并将树上结点的哈希值放入哈希表,查询直接二分 $p$,通过哈希值判断即可。

有修改用一个支持二分的数据结构维护即可。

下面的代码用的是 zkw 线段树,正常线段树或异常树状数组当然也可以。

注意放进哈希表的值也是哈希值,也就是说需要双哈希,一个用来在哈希表上寻址,一个最终用来比较是否一样。

当然为了稳也可以多哈希,比较是否一样时多比较几个哈希值即可。

实现里用了 unsigned long long 和 1 << 24 这两个模数,主要的好处是不用模,并且不用刻意的维护两个数,只要把 unsigned long long 模出来的值与一下 (1 << 24) - 1 就可以得到另一个哈希值。(因为 $2 ^{24} | 2 ^{64}$)

DEBUG: 将 int 赋值为 unsigned long long 编译选项查不出来,拍了很久才拍出来…

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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**********************************************************
* Author : EndSaH
* Email : hjxhb1@gmail.com
* Created Time : 2019-08-29 20:12
* FileName : wib.cpp
* Website : https://endsah.cf
* *******************************************************/

#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <algorithm>

typedef unsigned long long ULL;
typedef long long LL;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
#define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++)
#define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch))
#define __bufsize (1 << 21 | 1)

char _ibuf[__bufsize], _obuf[__bufsize], _stk[50];
char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;

struct END
{ ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } }
__;

inline int read()
{
register int x = 0, flag = 1;
register char ch;
while (!isdigit(ch = getchar()))
if (ch == '-')
flag = -1;
while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar()));
return x * flag;
}

template <typename _INT>
inline void write(_INT x)
{
if (x < 0)
putchar('-'), x = -x;
while (*++stkpos = x % 10 ^ 48, x /= 10, x);
while (stkpos != _stk)
putchar(*stkpos--);
}

template <typename _Tp>
inline bool Chkmax(_Tp& x, const _Tp& y)
{ return x < y ? x = y, true : false; }

template <typename _Tp>
inline bool Chkmin(_Tp& x, const _Tp& y)
{ return x > y ? x = y, true : false; }

const int maxN = 5e5 + 2;
const int base = 5e5 + 7;

int n, m, q, rt, K;
ULL htree[maxN], pw[maxN];
ULL hsum[maxN << 2];
int Len[maxN << 2];
std::vector<int> G[maxN];

namespace Hashtable
{
typedef struct Chain* E;

const int And = (1 << 24) - 1;

int ecnt;
E head[And + 2];

struct Chain
{
int id;
ULL num;
E next;

Chain() { }

Chain(int _id, ULL _num, E _next) : id(_id), num(_num), next(_next) { }
} chain[maxN];

inline void Push(int id, ULL num)
{ head[num & And] = &(chain[ecnt++] = Chain(id, num, head[num & And])); }

inline int Check(int x, ULL val, int len)
{
ULL num = htree[x] * pw[len] + val;
for (E o = head[num & And]; o; o = o->next)
if (o->num == num)
return o->id;
return 0;
}
}
using Hashtable::Push;
using Hashtable::Check;

void DFS(int u)
{
Push(u, htree[u]);
for (int i = 0; i < (int)G[u].size(); ++i)
{
int v = G[u][i];
htree[v] = htree[u] * base + i + 1;
DFS(v);
}
}

inline ULL Merge(ULL left, ULL right, int len)
{ return left * pw[len] + right; }

inline void Pushup(int cur)
{ hsum[cur] = Merge(hsum[cur << 1], hsum[cur << 1 | 1], Len[cur << 1 | 1]); }

int Twopoint(int x, int cur, ULL sta, int len)
{
int ans = -1, tmp;
while (cur < K)
{
tmp = Check(x, Merge(sta, hsum[cur << 1], Len[cur << 1]), len + Len[cur << 1]);
if (tmp)
{
ans = tmp;
len += Len[cur << 1];
sta = Merge(sta, hsum[cur << 1], Len[cur << 1]);
(cur <<= 1) |= 1;
}
else
cur <<= 1;
}
return ans;
}

int Query(int x, int l, int r)
{
static std::vector<int> stk;
stk.clear();
int len = 0, ans = x, tmp;
ULL sta = 0;
for (l += K - 1, r += K + 1; l ^ r ^ 1; l >>= 1, r >>= 1)
{
if (~l & 1)
{
tmp = Check(x, Merge(sta, hsum[l ^ 1], Len[l ^ 1]), len + Len[l ^ 1]);
if (tmp)
{
ans = tmp;
sta = Merge(sta, hsum[l ^ 1], Len[l ^ 1]);
len += Len[l ^ 1];
}
else
{
tmp = Twopoint(x, l ^ 1, sta, len);
return tmp == -1 ? ans : tmp;
}
}
if (r & 1)
stk.push_back(r ^ 1);
}
while (stk.size())
{
int cur = stk.back();
stk.pop_back();
tmp = Check(x, Merge(sta, hsum[cur], Len[cur]), len + Len[cur]);
if (tmp)
{
ans = tmp;
sta = Merge(sta, hsum[cur], Len[cur]);
len += Len[cur];
}
else
{
tmp = Twopoint(x, cur, sta, len);
return tmp == -1 ? ans : tmp;
}
}
return ans;
}

void Input()
{
n = read(), m = read(), q = read();
for (K = 1; K <= m + 1; K <<= 1);
for (int i = 1; i <= n; ++i)
{
int fa = read();
if (!fa)
rt = i;
else
G[fa].push_back(i);
}
for (int i = 1; i <= m; ++i)
hsum[i + K] = read();
}

void Init()
{
DFS(rt);
pw[0] = 1;
for (int i = 1, tmp = std::max(n, m); i <= tmp; ++i)
pw[i] = pw[i - 1] * base;
for (int i = 0; i <= m + 1; ++i)
Len[i + K] = 1;
for (int i = K - 1; i; --i)
{
Len[i] = Len[i << 1] + Len[i << 1 | 1];
Pushup(i);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("wib.in", "r", stdin);
freopen("wib.out", "w", stdout);
#endif
Input(), Init();
while (q--)
{
int opt = read(), x = read();
if (opt == 1)
{
int l = read(), r = read();
write(Query(x, l, r)), putchar('\n');
}
else
{
hsum[x += K] = read();
for (x >>= 1; x; x >>= 1)
Pushup(x);
}
}
return 0;
}