CodeForces - 651C
Posted 华东交通ACM训练基地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 651C相关的知识,希望对你有一定的参考价值。
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.
Example
3
1 1
7 5
1 5
2
6
0 0
0 1
0 2
-1 1
0 1
1 1
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.
对题目给出的条件化简,得知满足条件的一对点的关系为(x1==x2||y1==y2)
直接用数组存储,然后每个点之间进行比较的时间复杂度为O(n^2),题目中n=2e5,因此要采用降低时间复杂度的方法,那么使用map是最佳的选择。
事实上,除去重合的点的情况,我们不需要知道点的两个坐标,只要知道一条线上有多少个点,若一条线有n个点,则该线上有n(n-1)/2对点满足题目条件。
那么考虑重合的点,若有n个点坐标为(x,y),那么在x线和y线上,这n个点被统计了两次,则减去n(n-1)/2对点即可。
综上,用三个map分别存储x坐标,y坐标和重复的点,代码如下:
以上是关于CodeForces - 651C的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces - 651C Watchmen(map)
[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段
Codeforces 86C Genetic engineering(AC自动机+DP)