#P1503. 鬼子进村

鬼子进村

Description

There are nn houses in the county connected by tunnels in a line. The ii-th house is connected only to the (i1)(i-1)-th and (i+1)(i+1)-th houses. Specifically, house 11 is connected only to house 22, and house nn is connected only to house n1n-1. There are mm messages arriving in order:

  1. If the message is D x: the enemy destroys house xx, and the tunnel is blocked.
  2. If the message is R: the villagers repair the most recently destroyed house.
  3. If the message is Q x: a soldier is trapped in house xx.

Define reachability as follows: if there exist houses i,ji, j (1ijn1 \leq i \leq j \leq n) such that for every kk with ikji \leq k \leq j, house kk is not destroyed, then house ii and house jj are mutually reachable.

Li Yunlong is nervous after receiving the information. He wants to know, for each trapped soldier, how many houses he can reach.

Input Format

The first line contains two integers n,mn, m.

Each of the next mm lines contains one of the three messages described above.

Output Format

For each trapped soldier, output the number of houses he can reach.

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

1
0
2
4

Hint

Constraints

1n,m5×1041 \leq n, m \leq 5 \times 10^4.

If a soldier is trapped in a destroyed house, he can only wait to die.

Translated by ChatGPT 5