首页 > 其他 > 详细

codeforces 650A Watchmen

时间:2016-03-16 19:12:57      阅读:224      评论:0      收藏:0      [点我收藏+]
A. Watchmen
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (xi, yi).

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 |xi - xj| + |yi - yj|. 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 iand 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 xi and yi (|xi|, |yi| ≤ 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
3
1 1
7 5
1 5
output
2
input
6
0 0
0 1
0 2
-1 1
0 1
1 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.

题意:问两个点之间的距离按照|1 - 7| + |1 - 5| 和技术分享这种方法算,结果相同的对数   分析:求横坐标相同或者纵坐标相同的点的对数即可,(注意减去横坐标纵坐标都相同的点)

 

#include<stdio.h>
#include<string.h>
#include<cstdio> 
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 200200
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
struct node
{
	LL x,y;
}s[MAX];
bool cmp(node a,node b)
{
	if(a.x==b.x)
	    return a.y<b.y;
	return a.x<b.x;
}
bool cmp1(node c,node d)
{
	if(c.y==d.y)
	    return c.x<d.x;
	return c.y<d.y;
}
int main()
{
	LL n,m,j,i,k,t;
	LL sum,sum1,sum2,sum3;
	while(scanf("%lld",&n)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%lld%lld",&s[i].x,&s[i].y);
		sum=0;
		sum1=sum2=sum3=1;	
		sort(s,s+n,cmp);
		for(i=0;i<n;i++)
		{
			j=i;
			sum1=0;
			while(j<n&&s[i].x==s[j].x)
			{
				sum1++;
				j++;
			}
			sum+=sum1*(sum1-1)/2;
			i=j-1;
		}
		sort(s,s+n,cmp1);
		for(i=0;i<n;i++)
		{
			sum2=0;
			j=i;
			while(s[i].y==s[j].y)
			{
				sum2++;
				j++;
			}				
			sum+=sum2*(sum2-1)/2;
			i=j-1;
		}
		for(i=0;i<n;i++)
		{
			sum3=0;
			j=i;
			while(s[i].x==s[j].x&&s[i].y==s[j].y)
			{
				sum3++;
				j++;
			}			   
			sum-=sum3*(sum3-1)/2;
			i=j-1;
		}
		printf("%lld\n",sum);
	}
	return 0;
}

  

codeforces 650A Watchmen

原文:http://www.cnblogs.com/tonghao/p/5284647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!