| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 2048 MB | 66 | 4 | 4 | 57.143% |
Ladder game is a popular game in Korea, as well as China and Japan. Wikipedia says that “It is known in Korean as Sadaritagi (사다리타기, literally "ladder climbing"), in Japanese as Amidakuji (阿弥陀籤, "Amida lottery"), and in Chinese as Guijiaotu (鬼腳圖, literally "ghost leg diagram").”
The diagram where the game is played consists of $n$ vertical lines with horizontal line segments connecting two adjacent vertical lines. The horizontal line segments are called legs. Each vertical line has a starting (upper) point and an end (lower) point. The basic rule of this game is simple as follows:
The vertical lines are numbered from 1ドル$ to $n$ from left to right. It is well known that the game result is a permutation of $[1, 2, \dots , n]$. For example, given a diagram with 4ドル$ vertical lines and 5ドル$ legs shown below, the game result is $[2, 3, 4, 1]$ from left to right.
However, some legs are redundant, meaning that the same result $[2, 3, 4, 1]$ can be achieved with fewer legs; as in the figure below, one can obtain the same result only with three legs excluding topmost and bottommost ones. We want to determine the minimum number of horizontal line segments (legs) needed to achieve the same result. Note that it is possible to draw new legs than the given ones if necessary.
You are given $q$ queries, where each query either adds a new leg or deletes an existing one. Write a program to output the minimum number of legs required to achieve the same game result of the ladder structure obtained after the query is processed. Note that each query is cumulative, meaning each subsequent query is applied to the ladder structure resulting from previous queries.
Your program is to read from standard input. The input starts with a line containing three integers $n,ドル the number of vertical lines, $m,ドル the initial number of legs, and $q,ドル the number of queries, separated by a space where 2ドル ≤ n ≤ 100,000円,ドル 1ドル ≤ m ≤ 100,000円,ドル and 1ドル ≤ q ≤ 100,000円$.
In the following $m$ lines, each line contains two positive integers $h$ and $a,ドル representing a leg at height $h$ connecting the $a$-th and $(a + 1)$-th vertical lines (1ドル ≤ a ≤ n - 1$). The vertical lines are ordered from left to right, and the height is numbered from top to bottom starting with 1ドル$. The height is no more than 10ドル^9$.
The next $q$ lines contain the query information. Each query is either in the form of A $h$ $a$ or D $h$ $a,ドル where 1ドル ≤ h ≤ 10^9,ドル 1ドル ≤ a ≤ n - 1$.
A $h$ $a$: add a leg at height $h$ between the $a$-th and $(a + 1)$-th vertical lines.D $h$ $a$: delete the leg at height $h$ between the $a$-th and $(a + 1)$-th vertical lines.You can assume that there are no contradictory operations, that is, existing legs will not be added, and non-existing legs will not be deleted. Also, you can assume that no two legs are positioned such that they share the endpoint of the same height and the same vertical line.
Your program is to write to standard output. The output consists of $q$ lines and each line contains the minimum number of legs required to achieve the same result for a query in the input order.
4 4 3 3 1 2 2 5 2 6 3 A 7 1 A 4 3 D 3 1
3 6 3
The sample input 1 gives the initial ladder structure below. The game result is $[3, 2, 4, 1]$.
After applying the first query A 7ドル$ 1ドル$ in the figure blow, the ladder structure is changed, then the game result becomes $[2, 3, 4, 1]$.
Among the five legs, only three legs (without topmost and bottommost legs) are enough to achieve the same game result $[2, 3, 4, 1]$ as shown in figure below, so the answer for the first query is 3ドル$.
After processing the second query A 4ドル$ 3ドル,ドル the ladder structure is changed as shown below. The number of legs cannot be further reduced. The answer for the second query is 6ドル$.
After applying the third query D 3ドル$ 1ドル,ドル the ladder structure is changed as shown below.
The ladder structure with three legs as shown below guarantees the same game result, so the answer for the third query is 3ドル$.
4 5 5 3 1 2 2 5 2 6 3 7 1 D 6 3 D 7 1 D 5 2 D 3 1 D 2 2
2 3 2 1 0
ICPC > Regionals > Asia Pacific > Korea > 2024 ICPC Asia Seoul Regional D번