codeforces #341 div 2 B. Wet Shark and Bishops

Posted by 111qqz on Monday, February 1, 2016

TOC

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
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=2E5+7;
const int C=1000;
struct point 
{
    int x,y;

    void in()
    {
    scanf("%d %d",&x,&y);
    }
}p[N];
int n ;
LL a[2005],b[2005];  //b :2..2000
            //a:-999 ..999
            //
LL cal( LL x)
{
    LL res = x*(x-1)/2;
    return res;
}
int main()
{
    #ifndef  ONLINE_JUDGE 
    freopen("code/in.txt","r",stdin);
  #endif

    cin>>n;
    for ( int i = 0 ;i < n ; i++) p[i].in();
	
    ms(a,0);
    ms(b,0);

    for ( int i = 0 ; i < n ; i++)
    {
        a[p[i].x-p[i].y+C]++;
        b[p[i].x+p[i].y]++;
    }
    LL ans = 0 ;
    for ( int i = 1 ; i <=2001 ; i++)
    {
        if (a[i]==0) continue;
        ans += cal(a[i]);
    }
    for ( int i = 1  ; i <= 2001 ; i ++)
    {
        if (b[i]==0) continue;
        ans += cal(b[i]);
    }
    cout<<ans<<endl;
	 

  #ifndef ONLINE_JUDGE  
  fclose(stdin);
  #endif
    return 0;
}

「真诚赞赏,手留余香」

111qqz的小窝

真诚赞赏,手留余香

使用微信扫描二维码完成支付