Under construction

Still being built. Things may change

The Sprawl

Time limit5sMemory limit768 MB

Summary
On an infinite grid, N cities grow one cell per day in index order; sum over all pairs the first day the two cities' territories touch and connect.
Level

Hard9 of 10

Topics
Graph, BFS, Geometry, Sorting
Solved
No attempts yet

Problem

Kim is playing a city-building simulation game on an infinite chessboard. Every cell is denoted by two integers (x,y)(x, y). Two cells are adjacent if they share an edge. Formally, cell (x,y)(x, y) is adjacent to four cells, {(x+1,y),(x,y+1),(x−1,y),(x,y−1)}\{(x+1, y), (x, y+1), (x-1, y), (x, y-1)\}.

In the beginning (day 0), city ii occupies the single cell (xi,yi)(x_i, y_i). If a cell is occupied by any city, it is called a developed cell. Thus, on day 0, there are exactly NN developed cells.

After each day, each city grows and expands by occupying any undeveloped adjacent cells. Formally, for each city ii, let SiS_i be the set of cells adjacent to at least one cell of city ii. Starting from city 1 to city nn, any undeveloped cell in SiS_i becomes part of city ii and becomes developed.

Two cities u,vu, v are "connected" when you can move from (xu,yu)(x_u, y_u) to (xv,yv)(x_v, y_v) passing only between adjacent developed cells. For two cities u,vu, v, let f(u,v)f(u, v) be the first day on which cities u,vu, v become connected. Kim is interested in each value of f(u,v)f(u, v), but there are too many values to consider. Thus, for the given starting cells, Kim only wants to calculate ∑1≤i<j≤Nf(u,v)\sum_{1 \leq i < j \leq N}{f(u, v)}.

Input

The first line contains the number of cities NN.

The next NN lines give the starting cell of the ii-th city as two integers xi,yix_i, y_i.

Output

Print ∑1≤u<v≤Nf(u,v)\sum_{1 \leq u < v \leq N}{f(u, v)}, where f(u,v)f(u, v) is the first day on which cities u,vu, v become connected.

Constraints

  • 1≤N≤2500001 \leq N \leq 250000
  • −107≤xi,yi≤107-10^7 \leq x_i, y_i \leq 10^7
  • All given cells are distinct

Examples2

  1. Example 1

    Input
    5
    -3 2
    0 0
    0 5
    1 2
    4 3
    
    Expected output
    19
    
  2. Example 2

    Input
    3
    0 2
    4 0
    0 0
    
    Expected output
    5