codeforces #341 div 2 B. Wet Shark and Bishops

http://codeforces.com/contest/621/problem/B

B. Wet Shark and Bishops

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.

Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

Input

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.

Each of next n lines contains two space separated integers x__i and y__i (1 ≤ x__i, y__i ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output

Output one integer — the number of pairs of bishops which attack each other.

Sample test(s)

input

5
1 1
1 5
3 3
5 1
5 5

output

6

input

3
1 1
2 3
3 5

output

0

Note

In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2),(1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.

题意:给出n个点,问在同一对角线上的有多少对。

思路:同一对角线上恒纵坐标和相同或者差相同。

/* ***********************************************
Author :111qqz
Created Time :2016年01月31日 星期日 22时02分40秒
File Name :code/cf/#341/B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=2E5+7;
 7const int C=1000;
 8struct point 
 9{
10    int x,y;
 1    void in()
 2    {
 3	scanf("%d %d",&x,&y);
 4    }
 5}p[N];
 6int n ;
 7LL a[2005],b[2005];  //b :2..2000
 8			//a:-999 ..999
 9			//
10LL cal( LL x)
11{
12    LL res = x*(x-1)/2;
13    return res;
14}
15int main()
16{
17	#ifndef  ONLINE_JUDGE 
18	freopen("code/in.txt","r",stdin);
19  #endif
	cin>>n;
	for ( int i = 0 ;i < n ; i++) p[i].in();
	
	ms(a,0);
	ms(b,0);
 1	for ( int i = 0 ; i < n ; i++)
 2	{
 3	    a[p[i].x-p[i].y+C]++;
 4	    b[p[i].x+p[i].y]++;
 5	}
 6	LL ans = 0 ;
 7	for ( int i = 1 ; i <=2001 ; i++)
 8	{
 9	    if (a[i]==0) continue;
10	    ans += cal(a[i]);
11	}
12	for ( int i = 1  ; i <= 2001 ; i ++)
13	{
14	    if (b[i]==0) continue;
15	    ans += cal(b[i]);
16	}
17	cout<<ans<<endl;
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}