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. Python3 json、pickle序列化与反序列化

    注意:可以dumps多次,loads只能一次,一般我们只dumps一次,loads一次,多个版本就写入多个文件 一.json序列化与反序列化: 支持各种语言数据交互,但只能处理字典,列表,集合等简单的 ...

  2. mysql之基本数据库操作(二)

    环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 mysql的启动.退出.重启 # 启动 $ sudo service mysqld start # 停止 $ sud ...

  3. [New Learn]被嫌弃的app的一生

    1.简介 为什么叫被嫌弃的app的一生?致敬电影<被嫌弃的松子的一生>. 自学IOS东一锄西一镐的总感觉没有一个总的概念,还是多看看官网吧,先看一下一个app的整个生命周期,本文主要是翻译 ...

  4. MySQL乐观锁

    MySQL悲观锁是依靠数据库的锁机制来实现,以实现最大程度上的独占性.但由于现代的web系统一般都是高并发的,所以悲观锁在这样的情况下的适用性不高,所以我们有了和悲观锁相对应的乐观锁. 乐观锁,是说假 ...

  5. Leetcode 之Simplify Path(36)

    主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...

  6. php各个版本的区别

    一. PHP 5.2.5.3.5.4.5.5.5.6 版本区别对比以及新功能详解 PHP5.2 以前:autoload, PDO 和 MySQLi, 类型约束 PHP5.2:JSON 支持 PHP5. ...

  7. ajax登录请求,无法跳转

    没有用form提交数据,用的ajax提交.服务器显示已经登录成功,并且返回了成功代码OK.却无法进行跳转: js代码: $("input[type='submit']").on(& ...

  8. P1466 集合 Subset Sums(01背包求填充方案数)

    题目链接:https://www.luogu.org/problem/show?pid=1466 题目大意:对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合, ...

  9. transition结合:after,:before实现动画

    div代码 <div class='div'> hover </div> css代码 .div{ width:200px; height:100px; line-height: ...

  10. Palindrome Partitioning——回溯算法的又一经典

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...