#abc328c. C - Consecutive

C - Consecutive

Score : 300300 points

问题描述

你将得到一个长度为 NN 的由小写英文字母组成的字符串 S=S1S2SNS = S_1S_2\ldots S_N

另外,你还将收到关于字符串 SSQQ 个查询。对于 i=1,2,,Qi = 1, 2, \ldots, Q,第 ii 个查询由两个整数 li,ril_i, r_i 表示,并询问以下内容:

在从第 lil_i 个字符到第 rir_i 个字符的子串 SliSli+1SriS_{l_i}S_{l_i+1}\ldots S_{r_i} 中,有多少位置连续出现了相同的英文小写字母?换句话说,有多少整数 pp 满足条件 lipri1l_i \leq p \leq r_i-1Sp=Sp+1S_p = S_{p+1}

请分别输出这 QQ 个查询的答案。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a string S=S1S2SNS = 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+1SriS_{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 lipri1l_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.
  • 1N,Q3×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.
  • 1liriN1 \leq l_i \leq r_i \leq N

Input

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

NN QQ

SS

l1l_1 r1r_1

l2l_2 r2r_2

\vdots

lQl_Q rQr_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, S3S4S9=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, S4S5S10=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

S1S2S5=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.

update @ 2024/3/10 01:52:39

}