Watchmen(Codeforces 650A)

原创 2016年05月31日 13:08:32

                                             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 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 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.

题意是问有多少对的欧几里得距离等于曼哈顿距离

进行两次排序

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX_N 200005
using namespace std;

struct node{
	__int64 x, y;
} dis[MAX_N];

bool cmp1(node a, node b) {
	if (a.x == b.x) {
		return a.y > b.y;
	}
	return a.x > b.x;
}
bool cmp2(node a, node b) {
	return a.y > b.y;
}

int main() {
	__int64 n;
	while (scanf("%I64d", &n) != EOF) {
		__int64 ans = 0;
		for (__int64 i = 0; i < n; i++) {
			scanf("%I64d%I64d", &dis[i].x, &dis[i].y);
		}
		__int64 cnt1 = 1, cnt2 = 1;
		sort(dis, dis + n, cmp1);
		for (__int64 i = 1; i < n;i++) {
			if (dis[i - 1].x == dis[i].x) {
				cnt1++;
				if (dis[i - 1].y == dis[i].y) 	cnt2++;
				else {
					ans -= (cnt2 - 1) * cnt2 / 2;
					cnt2 = 1;
				}
			}
			else {
				ans += cnt1 * (cnt1 - 1) / 2;
				cnt1 = 1;
				ans -= (cnt2 - 1) * cnt2 / 2;
				cnt2 = 1; 
			}	
		}
		ans += cnt1 * (cnt1 - 1) / 2 - cnt2 * (cnt2 - 1) / 2;
		sort(dis, dis + n, cmp2);
		cnt1 = 1;
		for (int i = 1; i < n; i++) {
			if (dis[i - 1].y == dis[i].y) {
				if (dis[i - 1].y == dis[i].y) cnt1++;
			}
			else {
				ans += cnt1 * (cnt1 - 1) / 2;
				cnt1 = 1;
			}
		}
		ans += cnt1 * (cnt1 - 1) / 2;
		printf("%I64d\n", ans);
 	}
	return 0;
}


 

版权声明:本文为博主原创文章,未经博主允许可以随便转载。 举报

相关文章推荐

Codeforces650A Watchmen

A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input ...

codeforces 650A Watchmen 【数学】

A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

codeforces #345 div.2 C Watchmen

C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input...

codeforces 651C Watchmen

C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input...

CodeForces 650B(二分)

B. Image Preview time limit per test 1 second memory limit per test 256 megabytes i...

Codeforces Zip-line 650D 345Div1D(LIS)

传送门 大意:给出一个序列,求修改一个数过后的最长上升子序列。 思路:可以用主席树在线搞,也可以用树状数组离线搞,明显后者好写得多。我们首先读取所有的询问,然后就把询问绑在给出的位置,然后我们正向...

Codeforces650B Image Preview

B. Image Preview time limit per test 1 second memory limit per test 256 megabytes inp...

【CodeForces】[651C]Watchmen

由题意可知满足条件的点 必然是x相同或者y相同 (可有等式化简得到)麻烦的地方在于存在 相同的点 所以计算时需要去除重复这一题写了有断断续续几个小时 所以思路有点混乱 到后面有点方 所以...

Codeforces Round #345 (Div. 2) C. Watchmen【规律】

C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard i...

Codeforces 651C - Watchmen

C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)