#jag2018summerday2j. [jag2018summer_day2_j]AB Sort
[jag2018summer_day2_j]AB Sort
Problem Statement
You have a string of length consisting of A
and B
. You have to process queries. Consider the -th query ( ). In this query you are given integers and . Then, for each ( ), is changed to the other letter, that is, A
becomes B
and B
becomes A
.
After each query, you have to calculate B
A
. Here, is a function which, given a string , returns a non-negative integer. The value of is defined as follows:
- Consider the following step.
- Step: For all substrings of which coincide with
BA
, replace them withAB
. All replacements are done at the same time.
- Step: For all substrings of which coincide with
- is the number of steps you need to perform until no substring of coincides with
BA
.
For example, ABAB
\= , BBAA
\= , and AAA
\= .
Constraints
- consists of
A
andB
. - are integers.
Input
Input is given from Standard Input in the following format:
:
Output
After each query, print the value of B
A
, one per line.
Sample Input 1
5
BAABA
2
1 3
0 2
Sample Output 1
6
6
After the first query, the string is BBBAA
, so print the value of BBBBAAA
in the first line. After the second query, the string is AAAAA
, so print the value of BAAAAAA
in the second line.
Sample Input 2
1
A
2
0 0
0 0
Sample Output 2
2
2