C. Watchmen

题目连接:

http://www.codeforces.com/contest/651/problem/C

Description

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.

Sample Input

3

1 1

7 5

1 5

Sample Output

2

Hint

题意

有n个点,然后让你算出有多少对点的欧几里得距离等于曼哈顿距离

题解:

平方之后,显然只要,只要(xi-xj)0或者(yi-yj)0就满足题意了

然后容斥一发,x相等+y相等-xy相等就好。

代码

#include<bits/stdc++.h>
using namespace std; map<int,long long>x;
map<int,long long>y;
map<pair<int,int>,long long>xy;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int X,Y;
scanf("%d%d",&X,&Y);
x[X]++;
y[Y]++;
xy[make_pair(X,Y)]++;
}
map<int,long long>::iterator it;
long long ans = 0;
for(it=x.begin();it!=x.end();it++)
{
long long p = it->second;
ans+=(p*(p-1)/2);
}
for(it=y.begin();it!=y.end();it++)
{
long long p = it->second;
ans+=(p*(p-1)/2);
}
map<pair<int,int>,long long>::iterator it2;
for(it2=xy.begin();it2!=xy.end();it2++)
{
long long p = it2->second;
ans-=(p*(p-1)/2);
}
cout<<ans<<endl;
}

Codeforces Round #345 (Div. 1) A - Watchmen 容斥的更多相关文章

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

  2. Codeforces Round #345 (Div. 1) A. Watchmen 模拟加点

    Watchmen 题意:有n (1 ≤ n ≤ 200 000) 个点,问有多少个点的开平方距离与横纵坐标的绝对值之差的和相等: 即 = |xi - xj| + |yi - yj|.(|xi|, |y ...

  3. Codeforces Round #345 (Div. 1) A. Watchmen (数学,map)

    题意:给你\(n\)个点,求这\(n\)个点中,曼哈顿距离和欧几里得距离相等的点对数. 题解: 不难发现,当两个点的曼哈顿距离等于欧几里得距离的时候它们的横坐标或者纵坐标至少有一个相同,可以在纸上画一 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #345 (Div. 2)

    DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h> typedef long ...

  6. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

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

  7. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  8. Codeforces Round #345 (Div. 2) C (multiset+pair )

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

  9. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

随机推荐

  1. 【DLL】动态库的创建,隐式加载和显式加载(转)

    原文转自:https://blog.csdn.net/dcrmg/article/details/53437913

  2. MACHINE_START与MACHINE_END【转】

    转自:http://blog.csdn.net/cxw3506/article/details/8475965 版权声明:本文为博主原创文章,未经博主允许不得转载. 在移植Linux时,有个结构体需要 ...

  3. Python基础===使用virtualenv创建一个新的运行环境

    virtualenv简直是一个神器,以ubuntu环境为例, 先安装virtualenv 然后执行如下命令: 多版本创建env的方式: virtualenv 虚拟环境文件目录名 python=pyth ...

  4. python的时间和日期--time、datetime应用

    time >>> import time >>> time.localtime() #以time.struct_time类型,打印本地时间 time.struct_ ...

  5. C基础 常用设计模式粗解

    引言 面向对象, 设计模式是现代软件开发基石. C的面向过程已经很简洁, 但不代表C就没有面向对象.(libuv框架中C面向对象用的很多) 因为思想是互通的.全当熟悉一下那些常用的设计模式.先假定有一 ...

  6. DOM编程艺术读书笔记 (须熟读)

    http://www.qdfuns.com/notes/39151/00d8bc6322359f00450f492ae56bf69e.html

  7. dotnet core多平台开发体验(mac os x 、windows、linux)

    前言 随着net core rc2的发布,园子里面关于net core的入门文章也也多了起来,但是大多数都是在一个平台上面来写几个简单的例子,或者是在解释代码本身,并没有体现说在一个平台上面创建一个项 ...

  8. Flash中的注册点和中心点

    用一句话概括注册点和中心点的作用,那就是:注册点用来定位,中心点用来变形 当然,这句话不是非常准确,只是暂时先这么理解,下面会详细讲解. 认识注册点 每个元件都有一个注册点.在元件编辑窗口.或在舞台中 ...

  9. python Mixin 是个啥?

    内容待添加... 参考文章: [1][python] Mixin 扫盲班

  10. vConsole ~ 移动开发调试工具

    在开发移动端项目时,有时候在PC端好好的,但是到了手机上出bug,很难调试,这时候可以用vConsole调试工具 使用方式 1.直接引入 <script src="vconsole.m ...