#P16315. [ICPC 2023 Jinan R] 基本子串结构

    ID: 18251 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>字符串2023哈希 hashingICPC济南Z 函数

[ICPC 2023 Jinan R] 基本子串结构

Problem Description

After finishing the paper Faster Algorithms for Internal Dictionary Queries, Xiaoqingyu and Qiyi Hadron decided to create the following problem.

Let lcp(s,t)\text{lcp}(s, t) denote the longest common prefix of strings s=s1s2sns = s_1 s_2 \dots s_n and t=t1t2tmt = t_1 t_2 \dots t_m, i.e., the largest integer kk such that 0kmin(n,m)0 \le k \le \min(n, m) and s1s2sks_1 s_2 \dots s_k equals t1t2tkt_1 t_2 \dots t_k.

Xiaoqingyu gives you a non-empty string s=s1s2sns = s_1 s_2 \dots s_n. Define $f(s) = \sum\limits_{i=1}^{n} \text{lcp}(s, \text{suf}(s, i))$, where suf(s,i)\text{suf}(s, i) denotes the suffix of ss starting from sis_i (i.e., suf(s,i)=sisi+1sn\text{suf}(s, i) = s_i s_{i+1} \dots s_n). Note that in this problem, the alphabet contains nn letters, not just 2626.

For each i=1,2,,ni = 1, 2, \cdots, n, you need to answer the following query: if you must change sis_i to another different character cc (csic \ne s_i), choose the best character cc and compute the maximum value of f(s(i))f(s^{(i)}), where s(i)=s1si1csi+1sns^{(i)} = s_1 \dots s_{i-1} c s_{i+1} \dots s_n.

Input Format

There are multiple test cases. The first line contains an integer TT denoting the number of test cases. For each test case:

The first line contains an integer nn (2n2×1052 \le n \le 2 \times 10^5), the length of the string.

The second line contains nn integers s1,s2,,sns_1, s_2, \dots, s_n (1sin1 \le s_i \le n), where sis_i means the ii-th character of the string is the sis_i-th letter in the alphabet.

It is guaranteed that the sum of all nn over the test cases does not exceed 2×1052 \times 10^5.

Output Format

Let m(i)m(i) denote the maximum value of f(s(i))f(s^{(i)}). To reduce the output size, for each test case output one integer on a single line, which is i=1n(m(i)i)\sum\limits_{i=1}^{n} (m(i) \oplus i), where \oplus is the bitwise XOR operator.

2
4
2 1 1 2
12
1 1 4 5 1 4 1 9 1 9 8 10
15
217

Hint

For the first sample, we first compute m(1)m(1).

  • If we change s1s_1 to 11, then f(s(1))=4+2+1+0=7f(s^{(1)}) = 4 + 2 + 1 + 0 = 7.
  • If we change s1s_1 to 33 or 44, then f(s(1))=4+0+0+0=4f(s^{(1)}) = 4 + 0 + 0 + 0 = 4.

Therefore, m(1)=7m(1) = 7.

Similarly, m(2)=6m(2) = 6, m(3)=6m(3) = 6, and m(4)=4m(4) = 4. So the answer is $(7 \oplus 1) + (6 \oplus 2) + (6 \oplus 3) + (4 \oplus 4) = 15$.

Translated by ChatGPT 5