Neighbor Distance

AtCoder
IDabc430_d
Time4000ms
Memory256MB
Difficulty
There is a number line, and initially person $0$ is standing alone at coordinate $0$. From now on, persons $1,2,\dots,N$ will arrive in this order and stand on the number line. Person $i$ stands at coordinate $X_i$. Here, $X_i \ge 1$, and $X_i$ is distinct for all persons. Each time a person arrives, answer the following question. * Suppose that currently $r+1$ persons $0,1,\dots,r$ are standing on the number line. * Here, define $d_i$ as the distance to the nearest other person from person $i$. * More formally, $\displaystyle d_i = \min_{0 \le j \le r, j \neq i} \vert X_i - X_j \vert$. * Find the sum of this $d$, that is, $\displaystyle \sum_{i=0}^r d_i$. ## Constraints * All input values are integers. * $1 \le N \le 5 \times 10^5$ * $1 \le X_i \le 10^9$ * $X_i \neq X_j$ if $i \neq j$. ## Input The input is given from Standard Input in the following format: $N$ $X_1$ $X_2$ $\dots$ $X_N$ [samples]
Samples
Input #1
10
5 2 7 4 108728325 390529120 597713292 322456626 845148281 812604915
Output #1
10
7
8
8
108728326
390529121
523096670
452057486
699492475
517144218

In this input, $10$ persons arrive.  
The first four persons are explained below.

*   When person $1$ arrives, there are persons at coordinates $0,5$.
    *   The required value is $5+5=10$.
*   When person $2$ arrives, there are persons at coordinates $0,2,5$.
    *   The required value is $2+2+3=7$.
*   When person $3$ arrives, there are persons at coordinates $0,2,5,7$.
    *   The required value is $2+2+2+2=8$.
*   When person $4$ arrives, there are persons at coordinates $0,2,4,5,7$.
    *   The required value is $2+2+1+1+2=8$.
API Response (JSON)
{
  "problem": {
    "name": "Neighbor Distance",
    "description": {
      "content": "There is a number line, and initially person $0$ is standing alone at coordinate $0$. From now on, persons $1,2,\\dots,N$ will arrive in this order and stand on the number line.   Person $i$ stands at ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc430_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There is a number line, and initially person $0$ is standing alone at coordinate $0$.\nFrom now on, persons $1,2,\\dots,N$ will arrive in this order and stand on the number line.  \nPerson $i$ stands at ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments