No.855 Snakes Sunbathing
タグ : / 解いたユーザー数 16
作問者 :
CuriousFairy315
/ テスター :
QCFium
Problem Statement
It is known that in the mountains at the center of Country 315 there is a valley shaped like a rectangle of size $W$×$H$.
$2(W+H)$ snakes have settled in this valley, and every snake lives in a burrow it has made in the wall of the valley.
Since snakes are reptiles, they try to sunbathe while the sun is up.
However, being timid, a snake immediately goes back into its burrow when it bumps into something.
You are now given $N$ queries.
In the $i$-th query, the snake whose burrow is in the wall at $(X_i, Y_i)$ pushes its body forward out of the burrow by $L_i$.
The snakes' bodies are sufficiently long, and a body never comes completely out of its burrow.
Also, when two snakes bump into each other, both of them immediately stop their current movement and go back into their burrows.
After all queries have finished, find for each snake how much of its body is out of its burrow, and output the sum of these values.
Supplement (you do not need to read this)
All snakes initially have no part of their bodies out of their burrows, and when a snake extends its body out of its burrow, it always extends in the direction of the valley as seen from the burrow.
The wall at every integer coordinate is inhabited by exactly one snake; there is never a burrow with no snake or with multiple snakes.
Also, the queries are processed one at a time, from top to bottom.
Please also check the samples for the behavior.
Input
$H$ $W$ $N$ $X_1$ $Y_1$ $L_1$ $\vdots$ $X_N$ $Y_N$ $L_N$
$1 \leq W, H \leq 10^9$
$1 \leq N \leq 10^5$
$1 \leq L_i \leq 10^9$
$1\leq X_i \leq W$ and $Y_i$ is $0$ or $H+1$, or $1 \leq Y_i \leq H$ and $X_i$ is $0$ or $W+1$
Note: $(X_i, Y_i)$ is one of $(0, Y_i)$, $(W+1, Y_i)$, $(X_i, 0)$, $(X_i, H+1)$, which lies outside the valley (the valley is the range from $(1, 1)$ to $(W, H)$)
Output
Output the sum, over all snakes, of the length of the body extended out of the burrow.
Print a newline at the end.
Sample
Sample 1
Input
3 5 4 2 0 2 4 4 3 0 2 3 0 2 1
Output
4
First, by the first query, the snake living at $(2, 0)$ extends its body from $(2, 0)$ to $(2, 2)$.
Next, by the second query, the snake living at $(4, 4)$ extends its body from $(4, 4)$ to $(4, 1)$.
Next, by the third query, the snake living at $(0, 2)$ tries to extend its body from $(0, 2)$ to $(3, 2)$, but it bumps into the first snake at $(2, 2)$ on the way, so both snakes pull their bodies back into their burrows.
Finally, by the fourth query, the snake living at $(0, 2)$ extends its body from $(0, 2)$ to $(1, 2)$.
Thus, the snake that extended its body from $(4, 4)$ to $(4, 1)$ has length $3$ and the snake that extended its body from $(0, 2)$ to $(1, 2)$ has length $1$, so output their sum, $4$.
Sample 2
Input
2 2 3 1 3 2 3 2 1 3 2 1
Output
0
If a snake that has already extended its body out of its burrow is specified, it extends its body further.
If it bumps into another snake as a result, it of course goes back into its burrow.
Sample 3
Input
10 10 1 0 1 100
Output
0
If a snake extends its body as far as another burrow, it bumps into the snake living in that burrow and goes back into its burrow.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。