Cindy’s Christmas Challenge
시간 제한1.5초메모리 제한2048 MB
R, B, G 공으로 이루어진 문자열의 각 부분 문자열마다 빨강 R개 뒤에 파랑 B개가 오도록 만드는 최소 편집 연산 횟수를 구한다.
문제
The Christmas season is approaching! The town of Who-ville is getting colorful again, and so are the houses of its citizens, the Whos.
Cindy Lou Who, a young girl living in the city, has a very peculiar Christmas tradition: Cindy goes through her neighborhood, door-to-door, asking for red and blue Christmas tree balls. She gathers all the balls she collects and arranges them in her backyard. This Christmas, Cindy collected red balls and blue balls, which she arranged in a sequence with all red balls first, followed by all blue balls. Cindy called this arrangement an -sequence.
Near Who-ville lives Grinch, a cranky and solitary creature who hates Christmas and envies the Whos’ holiday cheer. He sneaked into Cindy’s backyard at night and replaced her -sequence with an arbitrary sequence of red, blue, and green balls (which isn’t a color Cindy likes, as it doesn’t contrast well with Christmas trees).
The Whos, known for their cheerful Christmas spirit, immediately offered to help Cindy recover her -sequence. Although she was initially mad at whoever messed in her backyard, she came up with an idea to turn this into a joyful Christmas game.
The game works as follows. For each citizen, Cindy will pick a contiguous subsequence, , from Grinch’s sequence and ask the citizen for the minimum number of operations needed to transform the subsequence into an -sequence. An operation consists of adding, removing or replacing a ball at any position within the subsequence. Note that each subsequence is not actually transformed into an -sequence; the citizen must just inform the minimum number of operations required.
Your task is to compute, for each Who, the minimum number of operations required for their assigned contiguous subsequence.

As an example, for and , consider the picture above. At the top of the picture is Cindy’s original -sequence (1). Below that is the sequence after Grinch’s attack (2). A possible contiguous subsequence with and follows (3). Finally, a way to transform the subsequence into a -sequence using four operations is shown, replacing the first two balls with red balls and adding two blue balls at the end (4). Four is the minimum number of operations required for this contiguous subsequence.
입력
The first line contains two integers and (), indicating respectively the number of red balls and the number of blue balls in Cindy’s -sequence.
The second line contains a string () describing Grinch’s sequence. Each character in is one of the uppercase letters “R”, “B” or “G”, indicating respectively a red, blue or green ball.
The third line contains an integer () representing the number of citizens in Who-ville.
Each of the next lines describes a contiguous subsequence of with two integers and (), indicating that the subsequence is assigned to a citizen.
출력
For each contiguous subsequence described in the input, output a line with an integer indicating the minimum number of operations needed to transform the subsequence into an -sequence. Output the results in the same order that the corresponding subsequences appear in the input.