Under construction

Still being built. Things may change

Good Triangle

Time limit2sMemory limit1024 MB

Summary
Count the triples of distinct points for which some point T is equidistant from all three under Manhattan distance, with up to 500000 points.
Level

Medium7 of 10

Topics
Geometry, Combinatorics, Prefix sum
Solved
No attempts yet

Problem

You are given nn distinct points on the two dimensional plane.

The distance between two points P=(x1,y1)P=(x_1, y_1) and Q=(x2,y2)Q=(x_2, y_2) is d(P,Q)=∣x1−x2∣+∣y1−y2∣d(P, Q)=|x_1-x_2|+|y_1-y_2|.

Three distinct points U,V,WU, V, W form a good triangle if there exists a point TT such that d(U,T)=d(V,T)=d(W,T)d(U, T)=d(V, T)=d(W, T). TT does not have to be a lattice point.

Find the number of good triangles that can be formed from the given points.

Input

The first line contains NN. Each of the next NN lines contains two space-separated integers xi,yix_i, y_i, the coordinates of the ii-th point.

Output

Print one integer, the number of good triangles.

Constraints

  • 3≤N≤500 0003 \leq N \leq 500\,000
  • −109≤xi,yi≤109-10^9 \leq x_i, y_i \leq 10^9 (1≤i≤N1 \leq i \leq N)
  • (xi,yi)≠(xj,yj)(x_i, y_i) \neq (x_j, y_j) if i≠ji \neq j (1≤i,j≤N1 \leq i, j \leq N)
  • All values in the input are integers.

Examples2

  1. Example 1

    Input
    5
    1 -1
    1 5
    5 7
    1 3
    4 2
    
    Expected output
    9
    
  2. Example 2

    Input
    10
    -2 -1
    -2 2
    -1 -2
    -1 -1
    -1 1
    0 1
    1 -1
    1 2
    2 -1
    2 1
    
    Expected output
    108