传送门

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.

--------------------------------------------------------------------------------------

Solution

不难看出,

两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离<==> x1=x2或y1=y2

对于所有x与y坐标,统计在对应竖直线与水平线上的点的个数,再删除重合点造成的重复计数即可,当然也要统计重合点的数目。

Implementation

#include <bits/stdc++.h>
using namespace std; typedef long long LL; map<pair<int,int>,LL> m;
map<int,LL> cnt[]; int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=; i<n; i++){
int x, y;
cin>>x>>y;
cnt[][x]++; cnt[][y]++;
m[{x,y}]++;
}
LL ans=;
for(int i=; i<; i++)
for(auto it=cnt[i].begin(); it!=cnt[i].end(); it++){
ans+=it->second*(it->second-)/;
}
for(auto it=m.begin(); it!=m.end(); it++)
ans-=it->second*(it->second-)/;
cout<<ans<<'\n';
return ;
}

用range-for还可将for-head写得更简洁些:

#include <bits/stdc++.h>
using namespace std; typedef long long LL; map<pair<int,int>,LL> m;
map<int,LL> cnt[]; int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=; i<n; i++){
int x, y;
cin>>x>>y;
cnt[][x]++; cnt[][y]++;
m[{x,y}]++;
}
LL ans=;
for(int i=; i<; i++)
for(auto it:cnt[i])
ans+=it.second*(it.second-)/;
for(auto it:m)
ans-=it.second*(it.second-)/;
cout<<ans<<'\n';
return ;
}

---------------------------------------------------

记录这道题是为了复习STL containers

基础不牢,地动山摇。

Codeforces 650A Watchmen的更多相关文章

  1. (水题)Codeforces - 650A - Watchmen

    http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...

  2. codeforces Codeforces 650A Watchmen

    题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离 也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可. #include <stdio.h> #includ ...

  3. Watchmen CodeForces - 650A

    Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...

  4. [刷题codeforces]650A.637A

    650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...

  5. A. Watchmen(Codeforces 650A)

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

  6. CodeForces 651C Watchmen map

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

  7. codeforces 651C Watchmen

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

  8. codefroces 650A. Watchmen

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

  9. CodeForces - 651C Watchmen (去重)

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

随机推荐

  1. ant 自动构建血泪史

    1.  android.bat update project -p . -t xxx 其中: xxx 为 targetid 特别注意的是:  targetid 不等于 API Level.... 2. ...

  2. C#代码规范 .NET程序员需要提升的修养

    一.  环境设置  首先去除VS开发环境中的一些选项如下: 粘贴时调整缩进 将类型的左大括号置于新行 将方法的左大括号置于新行 将匿名方法的左大括号置于新行 将控制块的左大括号置于新行 将“else” ...

  3. POJ 1002 487-3279

    A - 487-3279 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  4. 全面理解HTTP

    URL与URI 我们经常接触到的就是URL了,它就是我们访问web的一个字符串地址,那么URI是什么呢?他们是什么关系呢? URL:uniform resource location 统一资源定位符U ...

  5. [转]20位活跃在Github上的国内技术大牛

    FROM : http://blog.csdn.net/yaoxtao/article/details/38518933 20位活跃在Github上的国内技术大牛 本文列举了20位在Github上非常 ...

  6. [IPSEC PKI]

    PKI: 对称加密 非对称加密(混合加密) 数字签名   理论概述: (1)预备知识 对称加密:加密密钥和揭秘蜜钥是同一个密钥 缺点:不适合在互联网上传输密钥 密钥维护工作量大 n(n-1)/2 : ...

  7. c语言 数组名是常量指针

    //数组名是常量指针 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include ...

  8. js学习第二篇简单语法

    字符串(String)字面量 可以使用单引号或双引号 数组(Array)字面量 定义一个数组: [40, 100, 1, 5, 25, 10] 对象(Object)字面量 定义一个对象: {first ...

  9. LeetCode-Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  10. Javascript的字面量对象以及如何拆解字面量对象

    简单的说,字面量对象提供了一直非常方便构建新对象的方式,它的格式非常简单且容易阅读.是大多数前端程序员在构建对象时比较推崇的一种方式. 格式: var person = { name : " ...