PivotOJ

Modern Machine

시간 제한: 2500ms메모리 제한: 1024MB출처: JOI 2022-2023 본선BOJ 27540

문제

Bitaro is given JOI machine as a birthday present. JOI machine consists of one ball, NN light tiles, and MM buttons. The light tiles are numbered from 11 to NN. When Bitaro turns the power on, Light tile ii (1 ≤ i ≤ N) emit light of color CiC_i (blue (B) or red (R)). The buttons are numbered from 11 to MM. If Bitaro pushes Button jj (1 ≤ j ≤ M), the following happen.

  1. The ball is placed on Light tile AjA_j.
  2. Light tile AjA_j becomes red (regardless of its original color).
  3. The following operations are performed until the ball is removed.
    • Let pp be the index of the light tile where the ball is currently placed.
    • If Light tile pp is blue,
      • Light tile pp becomes red. After that, if p=1p = 1, the ball is removed. Otherwise, the ball moves to Light tile p1p - 1.
    • If Light tile pp is red,
      • Light tile pp becomes blue. After that, if p=Np = N, the ball is removed. Otherwise, the ball moves to Light tile p+1p + 1.

Bitaro is interested in JOI machine. He plans to perform QQ experiments. In the kk-th experiment (1 ≤ k ≤ Q), after Bitaro turns the power on, Bitaro pushes Buttons Lk,Lk+1,,RkL_k, L_{k + 1}, \dots , R_k in this order. After Bitaro pushes a button, he will not push the next button and wait until the ball is removed.

Given information of JOI machine and the experiments, write a program which calculates, for each experiment, the number of light tiles whose colors are red when the experiment finishes.

입력

Read the following data from the standard input.

NN MM

C1C2CNC_1C_2 \cdots C_N

A1A_1 A2A_2 \cdots AMA_M

QQ

L1L_1 R1R_1

L2L_2 R2R_2

\vdots

LQL_Q RQR_Q

출력

Write QQ lines to the standard output. In the kk-th line (1 ≤ k ≤ Q), the output should contain the number of light tiles whose colors are red when the kk-th experiment finishes.

예제

예제 1

입력
5 1
RBRRB
4
1
1 1
출력
1

예제 2

입력
5 3
RBRBR
1 3 4
2
2 3
1 3
출력
5
0

예제 3

입력
10 3
BBRRBRBRRB
2 10 5
1
1 3
출력
2

예제 4

입력
10 10
RRRRRRRRRR
3 1 4 1 5 9 2 6 5 3
5
1 7
2 8
3 9
4 10
1 10
출력
4
8
10
0
9

예제 5

입력
10 10
RRRBBBBBBB
3 1 4 1 5 9 2 6 5 3
5
1 10
2 9
3 8
4 7
5 6
출력
2
6
0
10
7

예제 6

입력
30 10
RRRBBRBBBRBBBRBRBRRRRRBBBBRBRR
3 28 2 29 1 30 6 14 7 7
10
1 10
2 3
2 5
2 8
3 3
3 6
4 5
4 7
5 9
10 10
출력
21
15
15
4
17
16
14
20
12
23
코드를 제출하려면 로그인하세요.