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. 无缝衔接demo

    如题. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" con ...

  2. 增大dma的分配

    前言 项目中需要通过驱动与fpga通讯,获取fpga往内存里写的数据.因为数据量比较大,需要驱动分配600多M的内存给fpga来写数据,且因为是与fpga通讯,需要连续的内存,还得是uncached的 ...

  3. 爬虫===登陆CSDN的方法

    本文主要介绍csdn的登陆,可应用在爬虫上~ # -*- coding:utf-8 -*- import json import requestsfrom xlutils.copy import co ...

  4. Linux 内核通知链随笔【中】【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...

  5. sunos kernel src leakrs

    https://github.com/joede/libezV24 https://github.com/ysei/siriusSparcV8 https://github.com/omniti-la ...

  6. 【COGS2622】后缀平衡树

    这是个后缀平衡树的裸题.... 然后傻逼的我调了一下午. #include<bits/stdc++.h> typedef long long ll; using namespace std ...

  7. Leetcode 之Same Tree(48)

    用递归比较简单,这里用迭代的方式实现.注意什么时候返回true,什么时候返回false. bool isSameTree(TreeNode *p, TreeNode *q) { stack<Tr ...

  8. leetcode 之Reorder List(25)

    找到中间结点,将后半部分反转接入即可. ListNode *reoderList(ListNode* head) { if (head == nullptr || head->next == n ...

  9. 重装系统备份MYSQL数据(整库备份)

    今天要重装Windows 8系统,但是我的Mysql里面数据太多,要备份成sql文件实在太麻烦,于是我听说可以直接拷贝数据文件夹,所以就试了,结果还成果了. 具体如下: 我安装的时候把数据文件夹就放在 ...

  10. 利用SQL SERVER对比两张表的数据一致性

    CREATE TABLE [dbo].[A](    [ID] [int] NULL,    [NAME] [varchar](50) NULL,    [SEX] [varchar](50) NUL ...