No.151 Segment Fishing
Problem Statement
You go fishing in a fishing pond that is an interval $[0,N)$ of length $N$.
All fish move according to the following rules.
- A fish facing left at position $x$ at time $t$ moves to position $x−1$ at time $t+1$ without changing direction.
However, a fish with $x = 0$ (a fish that cannot move to position $x−1$) stays at the same position and turns to face right. - Similarly, a fish facing right at position $x$ at time $t$ moves to position $x+1$ at time $t+1$ without changing direction.
However, a fish with $x = N−1$ (a fish that cannot move to position $x+1$) stays at the same position and turns to face left.
Initially, at $t = 0$, there are no fish in the pond.
At each time $t$ ($1 \le t \le Q$), one of the following $3$ types of queries is given. Process the $Q$ queries in order.
The $t$-th query (1-indexed) is executed at time $t$.
- 'L y z' — $z$ fish are released at position $y$ facing left. (All $z$ fish face left.)
- 'R y z' — $z$ fish are released at position $y$ facing right. (All $z$ fish face right.)
- 'C y z' — Output the total number of fish in the interval $[y,z)$ at time $t$.
Input
$N$ $Q$ $x_1$ $y_1$ $z_1$ $\cdots$ $x_i$ $y_i$ $z_i$ $\cdots$ $x_Q$ $y_Q$ $z_Q$
The first line contains an integer $N$ ($2 \le N \le 2 \times 10^4$) representing the length of the interval and an integer $Q$ ($1 \le Q \le 2 \times 10^4$) representing the number of queries.
The following $Q$ lines, starting from line 2, each contain a query in one of the formats described in the problem statement.
The constraints on $x_i$,$y_i$,$z_i$ are as follows.
- When $x_i = L$ or $x_i = R$, $0 \le y_i < N$ and $1 \le z_i \le 10^8$.
- When $x_i = C$, $0 \le y_i < z_i \le N$.
Output
For the queries with $x_i = C$, output the answers in the order they are given.
Print a newline after each output.
Sample
Sample 1
Input
10 3 R 2 1 L 5 1 C 4 5
Output
2
The movement of the fish at each time is as follows (L denotes a fish facing left, R denotes a fish facing right, {...} denotes the list of fish at that position, and - denotes that there are no fish at that position.)
$t = 0$
----------
$t = 1$
--{R}-------
$t = 2$
---{R}-{L}----
$t = 3$
----{R,L}-----
Sample 2
Input
5 5 L 1 2 R 4 2 C 2 4 C 2 4 C 2 4
Output
0 2 4
The movement of the fish at each time is as follows.
$t = 0$
-----
$t = 1$
-{L,L}---
$t = 2$
{L,L}---{R,R}
$t = 3$
{R,R}---{L,L}
$t = 4$
-{R,R}-{L,L}-
$t = 5$
--{R,R,L,L}--
Sample 3
Input
4 3 R 3 3 C 0 4 C 1 2
Output
3 0
The movement of the fish at each time is as follows.
$t = 0$
----
$t = 1$
---{R,R,R}
$t = 2$
---{L,L,L}
$t = 3$
--{L,L,L}-
Sample 4
Input
10 10 C 3 8 L 1 2 R 7 3 C 1 5 C 0 6 L 2 3 R 3 5 C 2 10 R 7 1 L 6 1
Output
0 0 2 10
Sample 5
Input
20 20 R 5 3 L 11 4 R 18 1 R 10 1 R 16 2 R 12 4 C 4 5 C 2 16 L 14 5 C 3 19 C 3 9 R 8 5 C 15 16 C 1 2 L 10 2 L 11 4 L 0 3 R 18 2 C 6 13 C 8 10
Output
0 12 20 0 2 0 8 2
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
shimomire