codeforce 621 B. Wet Shark and Bishops

来源:互联网 发布:h3c配置vlan多个端口 编辑:程序博客网 时间:2024/05/16 11:26
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 a1000 by1000 grid. Both rows and columns of the grid are numbered from1 to1000. 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 integersxi andyi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column wherei-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.

Examples
Input
51 11 53 35 15 5
Output
6
Input
31 12 33 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



输入:第一个表示bishops的个数,接下来n行为bishops的坐标。

输出:对于处于同一条对角线的的bishop他们可以相互攻击,

解法:对于1000*1000的格子,共有4000多个对角线。分别扫描每条对角线存在的点的个数,然后对每条对角线求和。


AC:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
#define maxn 1300
#define ll __int64
bool vis[maxn][maxn];
int cnt[4*maxn];
vector< pair<int,int> > q;
int main()
{
    ll n;
    ll ans=0;
    scanf("%I64d",&n);
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    while(n--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        vis[x][y]=1;
    }
    for(int i=1;i<=1000;i++)
    {
        int x,y;
        x=i;y=1;
        while(x<=1000&&y<=1000)
        {
            if(vis[x][y]==1){cnt[i]++;}
            x++;y++;
        }
        x=i;y=1;
         while(x>=1&&y>=1)
        {
            if(vis[x][y]==1){cnt[1000+i]++;}
            x--;y++;
        }
        x=1;y=i+1;
        while(x<=1000&&y<=1000)
        {
            if(vis[x][y]==1){cnt[i+2000]++;}
            x++;y++;
        }
        x=1000;y=i+1;
         while(x>=1&&y<=1000)
        {
            if(vis[x][y]==1){cnt[3000+i]++;}
            x--;y++;
        }
    }
     for(int i=1;i<=4000;i++)
     {
         ans+=cnt[i]*(cnt[i]-1)/2;
     }
     printf("%I64d\n",ans);
}


0 0
原创粉丝点击