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.

题意就是曼哈顿距离和欧几里得距离,自己把公式随便算一算就得出要在同一个横坐标或者是同一个纵坐标的才成立,然后就可以用握手问题的那个公式,再把重复的相同的位置去掉就可以。

然而握手问题的公式我不会写。。。所以想了一个其他的,代码看一下就会懂的。。。

要用long long。。。

然后就是排序的时候要注意!!!别写错了==,wa在排序写错了。。。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=2*1e7;
typedef long long ll;
struct node{
int a,b;
}dis[N];
bool cmp1(node x,node y){
if(x.a==y.a)
return x.b<y.b;
return x.a<y.a;
}
bool cmp2(node x,node y){
if(x.b==y.b)
return x.a<y.a;
return x.b<y.b;
}
int main(){
int n;
ll cnt,ans;
while(~scanf("%d",&n)){
for(int i=0;i<n;i++)
scanf("%I64d%I64d",&dis[i].a,&dis[i].b);
sort(dis,dis+n,cmp1);
cnt=0;ans=0;
for(int i=1;i<n;i++){
if(dis[i].a==dis[cnt].a)
ans+=abs(i-cnt);
else
cnt=i;
}
cnt=0;
sort(dis,dis+n,cmp2);
for(int i=1;i<n;i++){
if(dis[i].b==dis[cnt].b)
ans+=abs(i-cnt);
else
cnt=i;
}
cnt=0;
for(int i=1;i<n;i++){
if(dis[i].a==dis[cnt].a&&dis[i].b==dis[cnt].b)
ans-=abs(i-cnt);
else
cnt=i;
}
printf("%I64d\n",ans);
}
return 0;
}

Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离的更多相关文章

  1. 曼哈顿距离、欧几里得距离、闵氏距离(p→∞为切比雪夫距离)

    曼哈顿距离: 是由十九世纪的赫尔曼·闵可夫斯基所创词汇 ,是种使用在几何度量空间的几何学用语,用以标明两个点在标准坐标系上的绝对轴距总和. 曼哈顿距离——两点在南北方向上的距离加上在东西方向上的距离, ...

  2. CodeForces 651 C Watchmen

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

  3. Codeforces 1093G题解(线段树维护k维空间最大曼哈顿距离)

    题意是,给出n个k维空间下的点,然后q次操作,每次操作要么修改其中一个点的坐标,要么查询下标为[l,r]区间中所有点中两点的最大曼哈顿距离. 思路:参考blog:https://blog.csdn.n ...

  4. Codeforces 491B. New York Hotel 最远曼哈顿距离

    最远曼哈顿距离有两个性质: 1: 对每一个点(x,y)  分别计算  +x+y , -x+y , x-y , -x-y 然后统计每种组合的最大值就能够了, 不会对结果产生影响 2: 去掉绝对值 , 设 ...

  5. Atitti knn实现的具体四个距离算法 欧氏距离、余弦距离、汉明距离、曼哈顿距离

    Atitti knn实现的具体四个距离算法  欧氏距离.余弦距离.汉明距离.曼哈顿距离 1. Knn算法实质就是相似度的关系1 1.1. 文本相似度计算在信息检索.数据挖掘.机器翻译.文档复制检测等领 ...

  6. 剑指Offer——网易笔试之解救小易——曼哈顿距离的典型应用

    剑指Offer--网易笔试之解救小易--曼哈顿距离的典型应用 前言 首先介绍一下曼哈顿,曼哈顿是一个极为繁华的街区,高楼林立,街道纵横,从A地点到达B地点没有直线路径,必须绕道,而且至少要经C地点,走 ...

  7. K-means真的不能使用曼哈顿距离吗?

    问题 说到k-means聚类算法,想必大家已经对它很熟悉了,它是基于距离计算的经典无监督算法,但是有一次在我接受面试时,面试官问了我一个问题:“k-means为什么不能使用曼哈顿距离计算,而使用欧式距 ...

  8. Hdu4311 || 4312Meeting point-1/-2 n个点中任意选一个点使得其余点到该点曼哈顿距离之和最小

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

随机推荐

  1. mysql yearweek修改开始日期

    MySQL 的yearweek函数默认是从周日~周六,需求需要从周一到周日,看了MySQL的文档后,按照如下使用即可更改开始日期. http://dev.mysql.com/doc/refman/5. ...

  2. 大数据Hadoop-1

    大数据Hadoop学习之搭建hadoop平台(2.2)   关于大数据,一看就懂,一懂就懵. 一.概述 本文介绍如何搭建hadoop分布式集群环境,前面文章已经介绍了如何搭建hadoop单机环境和伪分 ...

  3. 解决hadoop 集群启动常见错误办法

    hadoop 集群常见错误解决办法 hadoop 集群常见错误解决办法: (一)启动Hadoop集群时易出现的错误: 1.   错误现象:Java.NET.NoRouteToHostException ...

  4. 魔法物品(magic.pas/c/cpp)

    有两种类型的物品:普通物品和魔法物品.普通物品没有魔法属性,而魔法物品拥有一些魔法属性.每种普通物品有一个价值P,但每种魔法物品有两种价值:鉴定前的价值P.和鉴定后的价值P2(当然,P2总是大于P.) ...

  5. 洛谷P1346 电车

    P1346 电车 236通过 757提交 题目提供者yeszy 标签图论福建省历届夏令营 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 解不好啊,快疯了!!哪位大… 求解:为何除了-1的点之 ...

  6. java 保护内存操作的方法

    1.与c++不同,在java中,没有通过使用强制转换指针类型或者通过进行指针运算直接访问内存的方法.在java中使用对象时,需要严格地遵守类型规则.如果存在一个Mountain类对象的引用(类似于c+ ...

  7. ORACLE中根据生日得到年龄

    create or replace function F_GETAGE(dateofbirth date) return varchar2 is begin ) then ); else ) then ...

  8. 【uva1380 - 一个调度问题】思路题+树形dp

    [题意] 有n<=200个恰好需要一天完成的任务,要求用最少的时间完成所有任务.任务可以同时完成.但是有一些约束,分有向和无向两种,其中A-->B表示A必须在B前面完成,而A--B表示A和 ...

  9. Linux 工作站安全加固规范

    目标受众 这是一套 Linux 基金会为其系统管理员提供的推荐规范. 这个文档用于帮助那些使用 Linux 工作站来访问和管理项目的 IT 设施的系统管理员团队. 如果你的系统管理员是远程员工,你也许 ...

  10. django-crontab

    django-cromtab实现定时任务 参考:https://www.jianshu.com/p/1def9226158d ''' 首先安装插件:pip install django-crontab ...