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 嵌套函数

    嵌套函数: 函数体内用def定义函数 注意:函数体中调用其他函数不算嵌套函数,只能是函数的调用 简单的嵌套函数: 输出结果:

  2. Style2Paints:用AI技术为线稿快速上色的工具(GitHub 3310颗星)

    python 开源项目: Style2Paints:用AI技术为线稿快速上色的工具(GitHub 3310颗星) https://github.com/lllyasviel/style2paints

  3. 算法题之找出数组里第K大的数

    问题:找出一个数组里面前K个最大数. 解法一(直接解法): 对数组用快速排序,然后直接挑出第k大的数.这种方法的时间复杂度是O(Nlog(N)).N为原数组长度. 这个解法含有很多冗余,因为把整个数组 ...

  4. docker配置桥接网络

    [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cp ifcfg-et ...

  5. Git 分支 - 分支的新建

    https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA% ...

  6. ES6 一种新的数据结构--Map跟Objct的区别

    var map1=new Map(); var keys={key:'val'}; map1.set(keys,'content'); ==> {Object {key: "val&q ...

  7. redis之(六)redis的列表类型的命令

    [一]向列表两端添加元素 -->命令:LPUSH key value [value ...] -->向列表的左侧添加元素,返回值表示增加元素后列表的长度 -->命令:RPUSH ke ...

  8. PHP7.3发布啦

    作为PHP5的最后一个版本,也是目前使用最广泛的PHP版本,PHP 5.6始于公元2014年(不是1804年,嘿嘿),其第一个测试版PHP 5.6 alpha 1版于2014年1月发布.随机产生了第一 ...

  9. vue-touch不支持vue2.0的替换方法

    当你想用vue-touch时,却发现官网这句话 Touch events plugin for Vue.js. This plugin does not support Vue 2.0 yet. 但是 ...

  10. bzoj 1040 基向内环树dp

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...