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. e621. Activating a Keystroke When Any Child Component Has Focus

    Normally, a keystroke registered on a component is activated when the component has the focus. This ...

  2. erlang的catch和 try catch的初步猜测

    一. catch(Fun):似乎可以避免因为 函数Fun内的错误而造成的当前的进程的崩溃.

  3. Getting SharePoint objects (spweb, splist, splistitem) from url string

    You basically get anything in the object model with one full url: //here is the site for the url usi ...

  4. Systemd on ubuntu

    何为 systemd? systemd 是一个 Linux 下的系统和会话管理器,与 SysV 和 LSB 启动脚本兼容.systemd 提供了积极的并行处理能力,使用套接字(socket)和 D-b ...

  5. mybatis由浅入深day02_9.3.5使用生成的代码_9.4逆向工程注意事项

    9.3.5 使用生成的代码 需要将生成工程中所生成的代码拷贝到自己的工程中. 拷这4个到我们原来的spring_mybatis1216工程下 ItemsMapper.java package cn.i ...

  6. 如何让input number类型的标签不产生上下加减的按钮

    之前用 input type="number" 来放数字框,发现有个上下加减的东西,感觉不太好 这个容易出现0 然后减为负数 这种情况下怎么去掉右边的那个上下加减的小按钮呢?前端同 ...

  7. 处理URL为模块/控制器/方法的格式

    一个路由在不确定长度的情况下,控制器和方法不一定传参进来,可能只有模块,甚至模块都没有,这个时候需要特殊处理一下,默认模块用 index,默认控制器用index,默认方法用 index来处理URL. ...

  8. Centos下Nagios的安装与配置

    一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报 ...

  9. 关于KEIL仿真的虚拟串口讲解

    这个是最后的效果图,右下方是串口打印的设置 第一步:在程序上写入关于串口一的配置,以及初始化和串口输出的内容 第二步:需要的时候在进行配置,在OPTIONS OF TARGET一栏的c/c++中(其原 ...

  10. windows下配置nutch注意的问题

    1.为处理方便,直接在$nutch目录下创建一个名为url.txt文件,然后在文件里添加要搜索的网址,例如:http://www.sina.com.cn/,注意网址最后的"/"一定 ...