luogu#P3732. [HAOI2017] 供给侧改革

    ID: 1298 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017河南各省省选排序进制字典树 Trie

[HAOI2017] 供给侧改革

Background

Description

You investigated the supply-demand balance of an industry over the past n n periods. Each period is represented by a single digit, either 0 0 or 1 1 . Thus, this forms a 01 \texttt{01} string S S of length n n . To better understand the data, you need to answer several queries. Let data(L,R) \text{data}(L, R) denote: among all suffixes of S S whose starting positions lie in [L,R] [L, R] , the length of the longest common prefix of the two suffixes that achieve the maximum common prefix.

For each query L,R L, R , compute:

ans=Li<Rdata(i,R).ans = \sum_{L \leqslant i < R} \text{data}(i, R).

It is guaranteed that each character of S S is randomly generated as 0 0 or 1 1 .

Output Format

Output Q Q lines, each containing a single integer, the answer to the corresponding query.

6 3
010110
2 5
1 6
1 2
4
6
0

Hint

[Constraints and Notes]

Data Point Scale of n n Scale of Q Q
1,21, 2 20 \leqslant 20
3,43, 4 100 \leqslant 100
5,65, 6 5×103 \leqslant 5 \times 10^3
7,8,9,107, 8, 9, 10 105 \leqslant 10^5

For all testdata, it is guaranteed that n105 n \leqslant 10^5 , Q105 Q \leqslant 10^5 , 1L<Rn 1 \leqslant L < R \leqslant n , and the 01 \texttt{01} string is randomly generated.

Translated by ChatGPT 5