Watchmen
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (x**i, y**i).
They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |x**i - x**j| + |y**i - y**j|. Daniel, as an ordinary person, calculates the distance using the formula .
The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.
Input
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.
Each of the following n lines contains two integers x**i and y**i (|x**i|, |y**i| ≤ 109).
Some positions may coincide.
Output
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.
Examples
Input
31 17 51 5
Output
2
Input
60 00 10 2-1 10 11 1
Output
11
Note
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
法一分别按照x和y进行排序找x与y分别相同的个数,再减去既x相同又y相同的部分
AC代码:
#include<stdio.h>
using namespace std;
#include<algorithm>
long long int f(long long int x);
bool cmp(struct book a,struct book b);
bool cmp2(struct book a,struct book b);
struct book
{
long long int x;
long long int y;
}a[200005];
int main()
{
long long int n,ans=0,same=0,t=0,s=0,sam=0;
scanf("%lld",&n);
for(long long int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
}
sort(a+1,a+n+1,cmp);
for(long long int i=1;i<n;i++)
{
if(a[i].y==a[i+1].y&&a[i].x==a[i+1].x)//注意一定要有两个=否则错了很难看出来,注意这个要单独判断,不能与下面的if在一起判断 否则就会造成如果a[i].y!=a[i+1].y则sam不会置零
{
sam++;
}
else
{
same+=f(sam);
sam=0;
}
if(a[i].x==a[i+1].x)
{
t++;
if(i==n-1)//注意ans和same当i==n-1时都要特殊处理
{
ans+=f(t);
same+=f(sam);
}
}
else
{
ans+=f(t);
t=0;
}
}
sort(a+1,a+n+1,cmp2);
for(long long int i=1;i<n;i++)
{
if(a[i].y==a[i+1].y)
{
s++;
if(i==n-1)
ans+=f(s);
}
else
{
ans+=f(s);
s=0;
}
}
ans-=same;
printf("%lld",ans);
return 0;
}
bool cmp(struct book a,struct book b)
{
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
long long int f(long long int x)
{
return (x+1)*x/2;
}
bool cmp2(struct book a,struct book b)
{
if(a.y==b.y) return a.x<b.x;//???
return a.y<b.y;
}
法二利用STL里的map快速查找
AC代码:
#include<iostream>
#include <map>
using namespace std;
map<int,int>mp1,mp2;
map<pair<int,int>,int>mp;
int n,i,x,y;
long long ans;
int main()
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
ans+=mp1[x]++;
ans+=mp2[y]++;
ans-=mp[make_pair(x,y)]++;
}
printf("%I64d\n",ans);
}