Problem:
Score : 900 points

Problem Statement

There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).

We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).

You have a peculiar pair of compasses, called Manhattan Compass.
This instrument always points at two of the pinholes.
The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.

When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.

Initially, the compass points at the a-th and b-th pinholes.
Find the number of the pairs of pinholes that can be pointed by the compass.

Constraints

2≦N≦10^5

1≦x_i, y_i≦10^9

1≦a < b≦N

When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)

x_i and y_i are integers.

Input

The input is given from Standard Input in the following format:

N a b
x_1 y_1
:
x_N y_N

Output

Print the number of the pairs of pinholes that can be pointed by the compass.

Sample Input 1

5 1 2
1 1
4 3
6 1
5 5
4 8

Sample Output 1

4

Initially, the compass points at the first and second pinholes.

Since d(1,2) = d(1,3), the compass can be moved so that it will point at the first and third pinholes.

Since d(1,3) = d(3,4), the compass can also point at the third and fourth pinholes.

Since d(1,2) = d(2,5), the compass can also point at the second and fifth pinholes.

No other pairs of pinholes can be pointed by the compass, thus the answer is 4.

Sample Input 2

6 2 3
1 3
5 3
3 5
8 4
4 7
2 5

Sample Output 2

4

Sample Input 3

8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8

Sample Output 3

7

Solution in Go (1.6) (judge verdict: Time Limit Exceeded)

package main

import (
	"fmt"
	"sort"
)

var N int
var xs []int
var ys []int
var sortedX []int
var sortedY []int
// x, y, -> index
var xys = make(map[int]map[int]int)
var yxs = make(map[int]map[int]int)
// Whether points being checked.
var flags = make(map[int]map[int]bool)
var checkedPoints = make(map[int]bool)
// Reachable points.
//var points map[int]bool
var pace int
var Out int

// 2018-11-04T01:06:38+0800
// 2018-11-04T01:15:59+0800
// 2018-11-04T01:51:52+0800 # TLE @see https://arc065.contest.atcoder.jp/submissions/3527473
// 2018-11-04T02:04:30+0800 # TRY Limiting x and y.
// 2018-11-04T03:08:06+0800 # TLE but much better :) @see https://arc065.contest.atcoder.jp/submissions/3527581
// 2018-11-04T14:37:39+0800 # TRY Caching the distance.
// 2018-11-04T16:42:44+0800
func main() {
	var a, b int
	fmt.Scanf("%d%d%d", &N, &a, &b)
	a--
	b--
	flags = make(map[int]map[int]bool, N)
	//points = make(map[int]bool, N)
	xs = make([]int, N)
	ys = make([]int, N)
	// dis -> xs
	dises0 := make(map[int][]int, N)
	// dis -> xs -> index
	indexesByDis0 := make(map[int]map[int]int, N)
	// whether is sorted
	flags0 := make(map[int]bool, N)
	// dis = x -y -> xs
	dises2 := make(map[int][]int, N)
	indexesByDis2 := make(map[int]map[int]int, N)
	flags2 := make(map[int]bool, N)
	for i := 0; i < N; i++ {
		fmt.Scanf("%d%d", &xs[i], &ys[i])
		dis := xs[i] + ys[i]
		if dises0[dis] == nil {
			dises0[dis] = make([]int, 0)
			indexesByDis0[dis] = make(map[int]int)
		}
		dises0[dis] = append(dises0[dis], xs[i])
		indexesByDis0[dis][xs[i]] = i

		dis = xs[i] - ys[i]
		if dises2[dis] == nil {
			dises2[dis] = make([]int, 0)
			indexesByDis2[dis] = make(map[int]int)
		}
		dises2[dis] = append(dises2[dis], xs[i])
		indexesByDis2[dis][xs[i]] = i
	}

	pace = length(a, b)

	var cal func(cur, xFrom int)
	cal = func(cur, xFrom int) {
		//fmt.Println("found", cur+1, "from", xFrom+1)
		if checkedPoints[cur] {
			return
		}
		checkedPoints[cur] = true
		dist := xs[cur] + ys[cur] - pace
		if len(dises0[dist]) > 0 {
			if !flags0[dist] {
				sort.Ints(dises0[dist])
				flags0[dist] = true
			}
			start := sort.SearchInts(dises0[dist], xs[cur]-pace)
			//fmt.Println("1 Looking in", dises0[dist], start)
			if start < len(dises0[dist]) && dises0[dist][start] == xs[cur]-pace {
				start++
			}
			for i := start; i < len(dises0[dist]) && dises0[dist][i] < xs[cur]; i++ {
				Out++
				cal(indexesByDis0[dist][dises0[dist][i]], cur)
			}
		}

		dist = xs[cur] + ys[cur] + pace
		if len(dises0[dist]) > 0 {
			if !flags0[dist] {
				sort.Ints(dises0[dist])
				flags0[dist] = true
			}
			start := sort.SearchInts(dises0[dist], xs[cur])
			//fmt.Println("2 Looking in", dises0[dist], start)
			if start < len(dises0[dist]) && dises0[dist][start] == xs[cur] {
				start++
			}
			for i := start; i < len(dises0[dist]) && dises0[dist][i] < xs[cur]+pace; i++ {
				Out++
				cal(indexesByDis0[dist][dises0[dist][i]], cur)
			}
		}

		dist = xs[cur] - ys[cur] - pace
		if len(dises2[dist]) > 0 {
			if !flags2[dist] {
				sort.Ints(dises2[dist])
				flags2[dist] = true
			}
			start := sort.SearchInts(dises2[dist], xs[cur]-pace)
			//fmt.Println("3 Looking in", dises2[dist], start)
			for i := start; i < len(dises2[dist]) && dises2[dist][i] <= xs[cur]; i++ {
				Out++
				cal(indexesByDis2[dist][dises2[dist][i]], cur)
			}
		}

		dist = xs[cur] - ys[cur] + pace
		if len(dises2[dist]) > 0 {
			if !flags2[dist] {
				sort.Ints(dises2[dist])
				flags2[dist] = true
			}
			start := sort.SearchInts(dises2[dist], xs[cur])
			//fmt.Println("4 Looking in", dises2[dist], start)
			for i := start; i < len(dises2[dist]) && dises2[dist][i] <= xs[cur]+pace; i++ {
				Out++
				cal(indexesByDis2[dist][dises2[dist][i]], cur)
			}
		}
	}

	cal(a, -2)
	//cal(b, 0)

	fmt.Println(Out / 2)
}

func main3() {
	var a, b int
	fmt.Scanf("%d%d%d", &N, &a, &b)
	a--
	b--
	flags = make(map[int]map[int]bool, N)
	//points = make(map[int]bool, N)
	xs = make([]int, N)
	ys = make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scanf("%d%d", &xs[i], &ys[i])
		if xys[xs[i]] == nil {
			xys[xs[i]] = make(map[int]int)
		}
		xys[xs[i]][ys[i]] = i
		if yxs[ys[i]] == nil {
			yxs[ys[i]] = make(map[int]int)
		}
		yxs[ys[i]][xs[i]] = i
		flags[i] = make(map[int]bool)
	}

	sortedX = make([]int, len(xys))
	i := 0
	for k := range xys {
		sortedX[i] = k
		i++
	}
	i = 0
	sortedY = make([]int, len(yxs))
	for k := range yxs {
		sortedY[i] = k
		i++
	}
	sort.Ints(sortedX)
	sort.Ints(sortedY)

	pace = length(a, b)
	//points[a] = true
	//points[b] = true
	flags[a] = make(map[int]bool)
	flags[b] = make(map[int]bool)
	flags[a][b] = true
	flags[b][a] = true
	//Out = 2
	findx(a)
	findx(b)

	fmt.Println(Out / 2)
}

func findx(cur int) {
	if checkedPoints[cur] {
		return
	}
	checkedPoints[cur] = true
	start := sort.SearchInts(sortedX, xs[cur]-pace)
	//fmt.Println("Checking", cur, "starting from:", start, "of", sortedX)
	for i := start; i < len(sortedX) && sortedX[i] <= xs[cur]+pace; i++ {
		offset := pace - abs(sortedX[i]-xs[cur])
		if offset == 0 {
			//fmt.Println(cur, "expects", sortedX[i], "-", ys[cur]+offset, ys[cur]-offset)
			if next, ok := xys[sortedX[i]][ys[cur]]; ok {
				//fmt.Println("found", cur, "with", next, ":", sortedX[i], "-", ys[cur], offset)
				Out++
				findx(next)
			}
			continue
		}
		//fmt.Println(cur, "expects", sortedX[i], "-", ys[cur]+offset, ys[cur]-offset)
		if next, ok := xys[sortedX[i]][ys[cur]+offset]; ok {
			//fmt.Println("found", cur, "with", next, ":", sortedX[i], "-", ys[cur]+offset, offset)
			Out++
			findx(next)
		}
		if next, ok := xys[sortedX[i]][ys[cur]-offset]; ok {
			//fmt.Println("found", cur, "with", next, ":", sortedX[i], "-", ys[cur]-offset, offset)
			Out++
			findx(next)
		}
	}
}

func main2() {
	var a, b int
	fmt.Scanf("%d%d%d", &N, &a, &b)
	a--
	b--
	flags = make(map[int]map[int]bool, N)
	//points = make(map[int]bool, N)
	xs = make([]int, N)
	ys = make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scanf("%d%d", &xs[i], &ys[i])
		flags[i] = make(map[int]bool)
	}

	pace = length(a, b)
	//points[a] = true
	//points[b] = true
	flags[a] = make(map[int]bool)
	flags[b] = make(map[int]bool)
	flags[a][b] = true
	flags[b][a] = true
	Out = 1
	find(a)
	find(b)

	fmt.Println(Out)
}

func find(cur int) {
	//if flags[cur] == nil {
	//	flags[cur] = make(map[int]bool)
	//}
	for i := 0; i < N; i++ {
		if flags[cur][i] {
			// Already checked.
			continue
		}
		flags[i][cur] = true
		flags[cur][i] = true
		if length(cur, i) == pace {
			//fmt.Println("cur, i:", cur, i)
			Out++
			//points[i] = true
			find(i)
		}
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func length(a, b int) int {
	return abs(xs[a]-xs[b]) + abs(ys[a]-ys[b])
}