386 传统题

[ABC328C] 连续相同字符统计 (Consecutive)

时间限制
2000ms
内存限制
1024MiB
登顶 / 尝试
0/ 0
难度
2
    上传者 标签AtCoderABC328普及-前缀和字符串

[ABC328C] 连续相同字符统计 (Consecutive)

Score : 300300 points

Problem Statement

You are given a string S=S1S2…SNS = S_1S_2\ldots S_N of length NN consisting of lowercase English letters.

Additionally, you are given QQ queries about the string SS. For i=1,2,…,Qi = 1, 2, \ldots, Q, the ii-th query is represented by two integers li,ril_i, r_i and asks the following.

In the substring SliSli+1…SriS_{l_i}S_{l_i+1}\ldots S_{r_i} of SS, which ranges from the lil_i-th to the rir_i-th character, how many places are there where the same lowercase English letter occurs twice in a row? In other words, how many integers pp satisfy li≤p≤ri−1l_i \leq p \leq r_i-1 and Sp=Sp+1S_p = S_{p+1}?

Print the answer for each of the QQ queries.

Constraints

  • NN and QQ are integers.

  • 1≤N,Q≤3×1051 \leq N, Q \leq 3 \times 10^5

  • SS is a string of length NN consisting of lowercase English letters.

  • lil_i and rir_i are integers.

  • 1≤li≤ri≤N1 \leq l_i \leq r_i \leq N

Input

The input is given from Standard Input in the following format:

$N$ $Q$
$S$
$l_1$ $r_1$
$l_2$ $r_2$
$\vdots$
$l_Q$ $r_Q$

Output

Print QQ lines. For i=1,2,…,Qi = 1, 2, \ldots, Q, the ii-th line should contain the answer to the ii-th query.

Sample Input 1

11 4
mississippi
3 9
4 10
4 6
7 7

Sample Output 1

2
2
0
0

The answers to the four queries are as follows.

  • For the first query, S3S4…S9=S_3S_4\ldots S_9 = ssissip has two places where the same lowercase English letter occurs twice in a row: S3S4=S_3S_4 = ss and S6S7=S_6S_7 = ss.

  • For the second query, S4S5…S10=S_4S_5\ldots S_{10} = sissipp has two places where the same lowercase English letter occurs twice in a row: S6S7=S_6S_7 = ss and S9S10=S_9S_{10} = pp.

  • For the third query, S4S5S6=S_4S_5S_6 = sis has zero places where the same lowercase English letter occurs twice in a row.

  • For the fourth query, S7=S_7 = s has zero places where the same lowercase English letter occurs twice in a row.

Sample Input 2

5 1
aaaaa
1 5

Sample Output 2

4

S1S2…S5=S_1S_2\ldots S_5 = aaaaa has four places where the same lowercase English letter occurs twice in a row: S1S2=S_1S_2 = aa, S2S3=S_2S_3 = aa, S3S4=S_3S_4 = aa, and S4S5=S_4S_5 = aa.