AtCoder Grand Contest 059
A
My Last ABC Problem
AtCoder
⌀ 1970
原题
←B →
翻译中…

Problem Statement

翻译中…

Consider a string $t$ consisting only of characters A, B, and C. We can do the following operation with it:

翻译中…
  • Choose any substring $t[l:r]$ and any permutation $(X, Y, Z)$ of characters $($A$,$B$,$C$)$. Here, $t[l:r]$ denotes the substring formed by the $l$-th through the $r$-th characters of $t$, where $l$ and $r$ are of your choice. Then, replace each character A, B, and C in $t[l:r]$ by $X$, $Y$, and $Z$, respectively.
翻译中…

For example, for a string $t =$ ACBAAC, we can choose a substring $t[3:6]$ and $(X,Y,Z)=($C$,$B$,$A$)$. After this operation, the string will become ACBCCA.

翻译中…

Alina likes strings in which all characters are the same. She defines the beauty of a string $t$ as the minimum number of operations required to make all its characters equal.

翻译中…

You are given a string $S$ of length $N$ consisting only of characters A, B, and C. Answer $Q$ queries. The $i$-th query is the following:

翻译中…
  • Given integers $L_i$ and $R_i$, find the beauty of the substring $t=S[L_i:R_i]$.
翻译中…

Constraints

翻译中…
  • $1 \le N \le 10^5$
  • $S$ is a string of length $N$ consisting only of characters A, B, and C.
  • $1 \le Q \le 10^5$
  • $1 \le L_i \le R_i \le N$
  • All numbers in the input are integers.
翻译中…

Input

翻译中…

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

翻译中…

Output $Q$ lines. In the $i$-th line, output the answer to the $i$-th query.

翻译中…

样例

Input #1
6 4
ABCCCA
3 5
2 3
1 3
1 6
Output #1
0
1
2
2
In the first query, the string is $t =$ `CCC`, in which all letters are already equal. The answer is $0$. In the second query, the string is $t =$ `BC`. We can change it to `BB` in one operation, by choosing a substring $t[2:2]$ and $(X,Y,Z)=($`A`$,$`C`$,$`B`$)$. In the third query, the string is $t =$ `ABC`. We can change it to `AAB` in one operation, by choosing a substring $t[2:3]$ and $(X,Y,Z)=($`C`$,$`A`$,$`B`$)$, and then to `BBB` in the next operation, by choosing a substring $t[1:2]$ and $(X,Y,Z)=($`B`$,$`A`$,$`C`$)$.
题目信息
题号
agc059_a
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。