Under construction

Still being built. Things may change

Modern Machine

Time limit2.5sMemory limit1024 MB

Summary
Given N light tiles and M buttons, answer Q queries that ask how many tiles are red after pressing buttons L to R in order.
Level

Hard8 of 10

Topics
Simulation, Segment tree, Divide and conquer
Solved
No attempts yet

Problem

Bitaro was given a JOI machine as a birthday present. The 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≤N1 \le i \le N) emits light of color CiC_i (blue B or red R). The buttons are numbered from 11 to MM. When Bitaro pushes button jj (1≤j≤M1 \le j \le M), the following happens.

  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 steps repeat until the ball is removed.
    • Let pp be the index of the light tile where the ball currently is.
    • If light tile pp is blue, light tile pp becomes red. Then, if p=1p = 1, the ball is removed. Otherwise, the ball moves to light tile p−1p - 1.
    • If light tile pp is red, light tile pp becomes blue. Then, if p=Np = N, the ball is removed. Otherwise, the ball moves to light tile p+1p + 1.

Bitaro runs QQ experiments. In the kk-th experiment (1≤k≤Q1 \le k \le Q), Bitaro turns the power on, then pushes buttons Lk,Lk+1,…,RkL_k, L_{k+1}, \dots, R_k in this order. After he pushes a button, he waits until the ball is removed before pushing the next one. For each experiment, find the number of light tiles that are red when the experiment finishes.

Input

The input is given in the following format.

NN MM

C1C2⋯CNC_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

Output

Print QQ lines. On the kk-th line, print the number of light tiles that are red when the kk-th experiment finishes.

Constraints

  • 3≤N≤120 0003 \le N \le 120\,000.
  • 1≤M≤120 0001 \le M \le 120\,000.
  • CiC_i (1≤i≤N1 \le i \le N) is either B or R.
  • 1≤Aj≤N1 \le A_j \le N (1≤j≤M1 \le j \le M).
  • 1≤Q≤120 0001 \le Q \le 120\,000.
  • 1≤Lk≤Rk≤M1 \le L_k \le R_k \le M (1≤k≤Q1 \le k \le Q).
  • NN, MM, AjA_j, QQ, LkL_k, RkR_k are integers.

Examples5

  1. Example 1

    Input
    5 1
    RBRRB
    4
    1
    1 1
    
    Expected output
    1
    
  2. Example 2

    Input
    5 3
    RBRBR
    1 3 4
    2
    2 3
    1 3
    
    Expected output
    5
    0
    
  3. Example 3

    Input
    10 3
    BBRRBRBRRB
    2 10 5
    1
    1 3
    
    Expected output
    2
    
  4. Example 4

    Input
    10 10
    RRRRRRRRRR
    3 1 4 1 5 9 2 6 5 3
    5
    1 7
    2 8
    3 9
    4 10
    1 10
    
    Expected output
    4
    8
    10
    0
    9
    
  5. Example 5

    Input
    10 10
    RRRBBBBBBB
    3 1 4 1 5 9 2 6 5 3
    5
    1 10
    2 9
    3 8
    4 7
    5 6
    
    Expected output
    2
    6
    0
    10
    7