Cindy’s Christmas Challenge

시간 제한1.5초메모리 제한2048 MB

요약
R, B, G 공으로 이루어진 문자열의 각 부분 문자열마다 빨강 R개 뒤에 파랑 B개가 오도록 만드는 최소 편집 연산 횟수를 구한다.
난이도

어려움10점 중 8점

유형
문자열, 동적 계획법, 누적 합, 이분 탐색
정답자
아직 제출이 없습니다

문제

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 RR red balls and BB blue balls, which she arranged in a sequence with all red balls first, followed by all blue balls. Cindy called this arrangement an (R,B)(R, B)-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 (R,B)(R, B)-sequence with an arbitrary sequence SS 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 (R,B)(R, B)-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, S_L,S_L+1,…,S_US\_L, S\_{L+1}, \dots , S\_U, from Grinch’s sequence and ask the citizen for the minimum number of operations needed to transform the subsequence into an (R,B)(R, B)-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 (R,B)(R, B)-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 R=3R = 3 and B=2B = 2, consider the picture above. At the top of the picture is Cindy’s original (3,2)(3, 2)-sequence (1). Below that is the sequence SS after Grinch’s attack (2). A possible contiguous subsequence with L=3L = 3 and U=5U = 5 follows (3). Finally, a way to transform the subsequence into a (3,2)(3, 2)-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 RR and BB (1≤R,B≤1061 ≤ R, B ≤ 10^6), indicating respectively the number of red balls and the number of blue balls in Cindy’s (R,B)(R, B)-sequence.

The second line contains a string SS (1≤∣S∣≤5⋅1051 ≤ |S| ≤ 5 \cdot 10^5 ) describing Grinch’s sequence. Each character in SS is one of the uppercase letters “R”, “B” or “G”, indicating respectively a red, blue or green ball.

The third line contains an integer WW (1≤W≤1051 ≤ W ≤ 10^5) representing the number of citizens in Who-ville.

Each of the next WW lines describes a contiguous subsequence of SS with two integers LL and UU (1≤L≤U≤∣S∣1 ≤ L ≤ U ≤ |S|), indicating that the subsequence S_L,S_L+1,…,S_US\_L, S\_{L+1}, \dots , S\_U 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 (R,B)(R, B)-sequence. Output the results in the same order that the corresponding subsequences appear in the input.

예제4

  1. 예제 1

    입력
    3 2
    RRBGRB
    3
    3 5
    1 5
    1 3
    
    예상 출력
    4
    3
    2
    
  2. 예제 2

    입력
    2 3
    RRGRBR
    6
    1 3
    1 2
    1 4
    3 3
    3 5
    5 6
    
    예상 출력
    3
    3
    3
    5
    3
    4
    
  3. 예제 3

    입력
    5 6
    GRBBBB
    4
    4 5
    1 4
    5 6
    2 2
    
    예상 출력
    9
    8
    9
    10
    
  4. 예제 4

    입력
    1 1
    RB
    2
    1 1
    1 2
    
    예상 출력
    1
    0