P9353 [JOI 2023 Final] 现代机器 / Modern Machine
Description
Bitaro is given JOI machine as a birthday present. JOI machine consists of one ball, $N$ light tiles, and $M$
buttons. The light tiles are numbered from $1$ to $N$. When Bitaro turns the power on, Light tile $i$ ($1 \leq i \leq N$) emit light of color $C_i$ (blue ($\texttt B$) or red ($\texttt R$)). The buttons are numbered from $1$ to $M$. If Bitaro pushes Button $j$ ($1 \leq j \leq M$), the following happen.
1. The ball is placed on Light tile $A_j$.
2. Light tile $A_j$ becomes red (regardless of its original color).
3. The following operations are performed until the ball is removed.
Let $p$ be the index of the light tile where the ball is currently placed.
- If Light tile $p$ is blue,
Light tile $p$ becomes red. After that, if $p = 1$, the ball is removed. Otherwise, the ball moves to
Light tile $p − 1$.
- If Light tile $p$ is red,
Light tile $p$ becomes blue. After that, if $p = N$, the ball is removed. Otherwise, the ball moves
to Light tile $p + 1$.
Bitaro is interested in JOI machine. He plans to perform $Q$ experiments. In the $k$-th experiment ($1 \leq k \leq Q$),
after Bitaro turns the power on, Bitaro pushes Buttons $L_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 experi-
ment, the number of light tiles whose colors are red when the experiment finishes.
Input Format
Read the following data from the standard input.
> $N$ $M$
> $C_1\ C_2 \dots C_N$
> $A_1\ A_2 \dots A_M$
> $Q$
> $L_1\ R_1$
> $L_2\ R_2$
> $\dots$
> $L_Q\ R_Q$
Output Format
Write $Q$ lines to the standard output. In the $k$-th line ($1 \leq k \leq Q$), the output should contain the number of light tiles whose colors are red when the $k$-th experiment finishes.
Explanation/Hint
**【Sample Explanation #1】**
The first experiment proceeds as follows.
1. Bitaro pushes Button 1, and ball is placed on Light tile 4.
2. Light tile 4 becomes red. Since the original color of Light tile 4 is red, the color of Light tile 4 does not
change.
3. After that, the following operations are performed.
(1)Since the current color of Light tile 4 is red, Light tile 4 becomes blue, and the ball moves to Light
tile 5.
(2)Since the current color of Light tile 5 is blue, Light tile 5 becomes red, and the ball moves to Light
tile 4.
(3)Since the current color of Light tile 4 is blue, Light tile 4 becomes red, and the ball moves to Light
tile 3.
(4)Since the current color of Light tile 3 is red, Light tile 3 becomes blue, and the ball moves to Light
tile 4.
(5)Since the current color of Light tile 4 is red, the color of Light tile 4 becomes blue, and the ball
moves to Light tile 5.
(6)Since the current color of Light tile 5 is red, the color of Light tile 5 becomes blue, and the ball is
removed.
After the experiment, Light tile 1 is the only light tile whose current color is red. Therefore, output 1.
This sample satisfies the constraints of subtasks 1, 2, 3, 6, and 7.
**【Sample Explanation #2】**
For the first experiment, Light tiles 1, 2, 3, 4, 5 are the light tiles whose current colors are red after the experiment. Since there are five such light tiles, output 5.
For the second experiment, there is no light tile whose current color is red after the experiment. Therefore,
output 0.
This sample satisfies the constraints of subtasks 3, 6, and 7.
**【Sample Explanation #3】**
This sample satisfies the constraints of subtasks 1, 2, 3, 6, and 7.
**【Sample Explanation #4】**
This sample satisfies the constraints of subtasks 3, 4, 5, 6, and 7.
**【Sample Explanation #5】**
This sample satisfies the constraints of subtasks 3, 5, 6, and 7.
**【Sample Explanation #6】**
This sample satisfies the constraints of subtasks 6 and 7.
**【Constraints】**
For all test cases, it is guaranteed that:
- $3 \leq N \leq 1.2 \times 10^5$;
- $1 \leq M \leq 1.2 \times 10^5$;
- $C_i \in \{\texttt{B},\texttt{R}\}$;
- $1 \leq A_j \leq N$;
- $1 \leq Q \leq 1.2 \times 10^5$;
- $1 \leq L_k \leq R_k \leq M$;
- $N, M, A_j, Q, L_k, R_k$ are all integers。
**【Subtasks】**
**Subtasks are used in this problem**。
1. (3 points) $N,M \leq 300$,$Q = 1$。
2. (12 points) $N, M \leq 7000$,$Q = 1$。
3. (10 points) $Q \leq 5$。
4. (11 points) $N = 10$,$C_i = \texttt R$。
5. (26 points) There exists a positive integer $t \in [0, N]$,is guaranteed that $1 \leq i \leq t$,$C_i = \texttt R$;And $\forall t < i \leq N$,$C_i = \texttt B$。
6. (17 points) $A_j \leq 20$ or $A_j > N - 20$。
7. (21 points) No more constraints。