The Sprawl

무한 격자에서 도시들이 하루에 한 겹씩 번호 순서대로 성장할 때, 모든 도시 쌍이 처음 연결되는 날짜의 합을 구한다.

어려움8그래프BFS기하유니온 파인드아직 제출이 없습니다시간 제한5초메모리 제한768 MB

문제

Kim is playing a city-building simulation game in an infinite chessboard. Every cell is denoted with two integer (x,y)(x, y). Two cells are adjacent if they share an edge - formally, cell (x,y)(x, y) is adjacent with four cells, (x+1,y),(x,y+1),(x1,y),(x,y1)\\{(x+1, y), (x, y+1), (x-1, y), (x, y-1)\\}.

In the beginning (day 0), city ii occupies the single cell (x_i,y_i)(x\_i, y\_i). If a cell is occupied by any city, then it’s called  *developed cells*. Thus, in day 0, there is exactly NN developed cells.

After each day, the city will grow and expand itself by occupying any undeveloped adjacent cells. Formally, for each city ii, let S_iS\_i be the set that is adjacent to at least one cell in city ii. Starting from city 1 to city nn, any undeveloped cell in S_iS\_i becomes the part of city ii, and become developed.

We call two city u,vu, v “connected”, when you can move between (x_u,y_u)(x\_u, y\_u) to (x_v,y_v)(x\_v, y\_v) by only passing between adjacent developed cells. For two city u,vu, v, let f(u,v)f(u, v) the first day where two city u,vu, v become connected. Kim is interested in each values of f(u,v)f(u, v), but there are too much values to consider. Thus, Kim only want to calculate _1i<jNf(u,v)\sum\_{1 \leq i < j \leq N}{f(u, v)} for a given first developed cells.

입력

The first line contains the number of city NN.

In next NN lines, the position of ii-th city’s starting cell is given as two integer x_i,y_ix\_i, y\_i.

출력

Print _1u<vNf(u,v)\sum\_{1 \leq u < v \leq N}{f(u, v)}, when f(u,v)f(u, v) is the first day where two city u,vu, v become connected.

제한

  • 1N2500001 \leq N \leq 250000 
  • 107x_i,y_i107-10^7 \leq x\_i, y\_i \leq 10^7 
  • All given cells are distinct