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个点,问在同一对角线上的有多少对。
思路:同一对角线上恒纵坐标和相同或者差相同。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年01月31日 星期日 22时02分40秒
4File Name :code/cf/#341/B.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=2E5+7;
34const int C=1000;
35struct point
36{
37 int x,y;
38
39 void in()
40 {
41 scanf("%d %d",&x,&y);
42 }
43}p[N];
44int n ;
45LL a[2005],b[2005]; //b :2..2000
46 //a:-999 ..999
47 //
48LL cal( LL x)
49{
50 LL res = x*(x-1)/2;
51 return res;
52}
53int main()
54{
55 #ifndef ONLINE_JUDGE
56 freopen("code/in.txt","r",stdin);
57 #endif
58
59 cin>>n;
60 for ( int i = 0 ;i < n ; i++) p[i].in();
61
62 ms(a,0);
63 ms(b,0);
64
65 for ( int i = 0 ; i < n ; i++)
66 {
67 a[p[i].x-p[i].y+C]++;
68 b[p[i].x+p[i].y]++;
69 }
70 LL ans = 0 ;
71 for ( int i = 1 ; i <=2001 ; i++)
72 {
73 if (a[i]==0) continue;
74 ans += cal(a[i]);
75 }
76 for ( int i = 1 ; i <= 2001 ; i ++)
77 {
78 if (b[i]==0) continue;
79 ans += cal(b[i]);
80 }
81 cout<<ans<<endl;
82
83
84 #ifndef ONLINE_JUDGE
85 fclose(stdin);
86 #endif
87 return 0;
88}