#P4786. [BalkanOI 2018] Election

[BalkanOI 2018] Election

Description

Translated from BalkanOI 2018 Day 1 T1 “Election”.

There is a string S[1N]S[1\dots N] of length NN, consisting only of the two letters C and T.
Now there are QQ queries. Each query contains two integers LL and RR, meaning: let the new string be S=S[LR]S' = S[L\dots R]. What is the minimum number of characters that must be deleted from SS' to ensure that, for every prefix and every suffix of SS', the number of C is not less than the number of T?

Input Format

The first line contains an integer NN.
The second line contains a string SS of length NN.
The third line contains an integer QQ.
In the next QQ lines, each line contains two integers LL and RR, representing one query.

Output Format

For each query, output one line: the minimum number of characters that must be deleted from SS' to satisfy the requirement in the statement.

11
CCCTTTTTTCC
3
1 11
4 9
1 6
4
6
3

Hint

Sample Explanation

Query 1: CCCTTTTTTCC.
Query 2: TTTTTT.
Query 3: CCCTTT.

Subtask #1 (28 points): 1N,Q20001 \le N, Q \le 2000.
Subtask #2 (54 points): 1N,Q7×1041 \le N, Q \le 7 \times 10^4.
Subtask #3 (18 points): 1N,Q5×1051 \le N, Q \le 5 \times 10^5.

Thanks to Planet6174 for providing the translation.

Translated by ChatGPT 5