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

两种距离相等的情况就是两个点的横坐标相等或者纵坐标相等。这里分别排序一下,然后注意要减去两个点相同的情况

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 200000
struct Node
{
long long int x;long long int y; }a[MAX+5];
long long int ans[MAX+5];
int cmp(Node a,Node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int cmp2(Node a,Node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int n;
long long int ans2[MAX+5];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,cmp);
memset(ans,0,sizeof(ans));
int cnt=0;
int cot=0;
ans[0]=1;
ans2[0]=1;
long long int num=1;
for(int i=2;i<=n;i++)
{
//cout<<a[i].x<<" "<<a[i-1].x<<endl;
if(a[i].x==a[i-1].x)
{
if(a[i].y==a[i-1].y)
ans2[cot]++;
else
{
cot++;
ans2[cot]=1;
}
ans[cnt]++;
}
else
{
cot++;
ans2[cot]=1;
cnt++;
ans[cnt]=1;
}
}
long long int res=0;
long long int res2=0;
for(int i=0;i<=cnt;i++)
{
res+=(ans[i]*(ans[i]-1))/2; }
for(int i=0;i<=cot;i++)
{
res2+=(ans2[i]*(ans2[i]-1))/2; } sort(a+1,a+n+1,cmp2);
memset(ans,0,sizeof(ans));
cnt=0;
ans[0]=1;
for(int i=2;i<=n;i++)
{
if(a[i].y==a[i-1].y)
{
ans[cnt]++;
}
else
{
cnt++;
ans[cnt]=1;
}
}
for(int i=0;i<=cnt;i++)
res+=(ans[i]*(ans[i]-1))/2;
printf("%lld\n",res-res2);
return 0;
}

CodeForces 651 C Watchmen的更多相关文章

  1. Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离

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

  2. codeforces 650 C. Watchmen(数学公式)

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

  3. 【CodeForces - 651C 】Watchmen(map)

    Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...

  4. CodeForces 651 A Joysticks

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Codeforces 651 B. Beautiful Paintings

    B. Beautiful Paintings   time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

  7. Watchmen CodeForces - 650A

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

  8. Codeforces Round #345 (Div. 1) A. Watchmen

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

  9. Codeforces 650A Watchmen

    传送门 time limit per test 3 seconds memory limit per test 256 megabytes input standard input output st ...

随机推荐

  1. 控制反转是Spring框架的核心。

    早在2004年,Martin Fowler就提出了“哪些方面的控制被反转了?”这个问题.他总结出是依赖对象的获得被反转了.基于这个结论,他为控制反转创造了一个更好的名字:依赖注入.许多非凡的应用(比H ...

  2. C# HttpRequest基础连接已经关闭: 接收时发生意外错误

    在c#中使用HttpWebRequest时,频繁请求一个网址时,过段时间就会出现“基础连接已经关闭: 接收时发生意外错误”的错误提示. 将webRequest的属性设置成下面的,经测试可以解决. we ...

  3. 让Android 变身回一台真正的Linux系统吧!!!

    在Android上开发也有两年的时间了,虽然一直都知道Android是构建在Linux Kernel上的手机操作系统,但在此之前一直没有实感. 直到第一次买了Android的手机,并请人帮我Root后 ...

  4. 不错的源码演示:admin5源码

    admin5源码官网:http://down.admin5.com/ 集资讯,源码,字体,itchat于一体的网站,可以下载喔! 实际项目中可以用得到.

  5. 重载 CreateParams 方法[2]: 重载 TForm.CreateParams 方法的几个例子

    这里有所有相关参数的解释: http://www.cnblogs.com/del/archive/2008/04/15/1154359.html //最大化窗口 procedure TForm1.Cr ...

  6. centos7下git的使用和配置

    1.下载git,使用命令: yum install git 2.配置git: git config --global user.name "Your Name" git confi ...

  7. mysql数据库中,查看某个数据库下的表的存储类型都有哪些

    需求描述: 在备份数据库的时候,使用mysqldump进行数据库的备份,如果库中仅仅有innodb存储引擎, 那么使用--single-transaction就可以,如果还有其他的存储引擎类型就要使用 ...

  8. How do I commit all deleted files in Git?

    Try this: $ git add -u This tells git to automatically stage tracked files -- including deleting the ...

  9. Makefile--隐含规则自动推dao(一)

    [版权声明:转载请保留出处:周学伟:http://www.cnblogs.com/zxouxuewei/] 上一节的Makefile勉强可用,但还写的比较繁琐,不够简洁.对每一个.c源文件,都需要写一 ...

  10. tiny4412 ubuntudesktop更新源(old)

    1.报错:404  Not Found [IP: 91.189.88.151 80] 2. deb http://old-releases.ubuntu.com/ubuntu/ raring main ...