No.728 Give and Take
タグ : / 解いたユーザー数 62
作問者 :
Problem Statement
There are $N$ houses on a number line. The coordinate of the $i$-th house is $A_i$, and each house has 2 children.
Of the children of house $i$, one moves $L_i$ in the negative direction along the number line and the other moves $R_i$ in the positive direction, handing out sweets to the houses they pass along the way.
Here, for each $i$, if a house exists at coordinate $A_i - L_i$ or $A_i + R_i$, the children of house $i$ are considered to have passed that house.
In other words, the children of house $i$ give sweets to the houses located in $[A_i-L_i, A_i+R_i]$.
How many pairs ( $i$ , $j$ ) ( $i < j$ ) are there such that house $i$ receives sweets from the children of house $j$ and house $j$ receives sweets from the children of house $i$?
Assume that the children of each house have sufficiently many sweets.
(Note) It has been confirmed that AC is achievable with C++ and PyPy3 (1823ms).
Input
$N$ $A_1 \ A_2 \cdots A_N$ $L_1 \ R_1$ $L_2 \ R_2$ $\vdots$ $L_N \ R_N$
$N$ , $A_i$ , $L_i$ , $R_i$ are integers satisfying the following.
$2 \le N \le 3 \times 10^5$
$0 \le A_1 < A_2 < \cdots < A_N \le 10^9$
$0 \le L_i,R_i \le 10^9$
Output
Output the number of $(i,j)\in \{1,2,\dots,N\}^2, i < j$ such that house $i$ receives sweets from the children of house $j$ and house $j$ receives sweets from the children of house $i$.
Note that the answer may not fit in a 32-bit integer type.
Print a newline at the end.
Sample
Sample 1
Input
4 1 2 4 7 3 5 0 3 3 4 2 2
Output
2
There are 2 pairs ( $i$ , $j$ ): (1, 3) and (2, 3).
Sample 2
Input
2 0 3 1 2 5 4
Output
0
No such pair ( $i$ , $j$ ) exists.
Sample 3
Input
6 1 2 3 4 5 9 1 10 7 3 5 6 3 4 3 9 3 0
Output
9
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。