#P4384. [八省联考 2018] 制胡窜

    ID: 3342 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串2018线段树倍增各省省选

[八省联考 2018] 制胡窜

Description

For a string ss, define s|s| to denote the length of ss.

Next, define sis_i as the ii-th character of ss, and sl,rs_{l,r} as the string formed by concatenating, from left to right, the ll-th through the rr-th characters of ss. In particular, if l>rl \gt r, or l[1,s]l \notin [1, |s|], or r[1,s]r \notin [1, |s|], we consider sl,rs_{l,r} to be the empty string.

Given a string ss of length nn consisting only of digits, there are qq queries. In the kk-th query, a substring sl,rs_{l,r} of ss is given. For this substring, count the number of pairs (i,j)(i, j) such that 1i<jn1 \le i \lt j \le n, i+1<ji + 1 < j, and sl,rs_{l,r} occurs in s1,is_{1,i} or in si+1,j1s_{i+1,j-1} or in sj,ns_{j,n}.

Input Format

The first line contains two integers, the string length nn and the number of queries qq.

The second line contains a string of length nn consisting only of digit characters, representing ss.

Each of the next qq lines contains two positive integers ll and rr, indicating that the queried substring is sl,rs_{l,r}.

Output Format

For each query, output a single integer on its own line representing the answer.

5 2
00100
1 2
1 3

5
1

Hint

Test point nn qq Other conditions
11 =50=50 =100=100 None
232 \sim 3 =300=300
454 \sim 5 =2000=2000 =3000=3000
696 \sim 9 =100000=100000 sl,r106\sum \lvert s_{l,r} \rvert \le 10^6
101210 \sim 12 =30000=30000 =50000=50000 None
1313 =100000=100000 =100000=100000 The string ss contains only 00.
142014 \sim 20 =300000=300000 None

For all testdata, 1n1051 \le n \le 10^5, 1q3×1051 \le q \le 3 \times 10^5, 1lrn1 \le l \le r \le n, and ss contains only digit characters.

Translated by ChatGPT 5