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
Note

In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and  for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.

思路:(|i.x-j.x|+|i.y-j.y|)^2=(i.x-j.x)^2+(i.y-j.y)^2+2*|i.x-j.x|*|i.y-j.y|=(i.x-j.x)^2+(i.y-j.y)^2;

    所以满足条件i.x==j.x或i.y==j.y至少一个;两次分别对x和y排序找到相同的有多少,最后再减去x和y同时相同的就是结果了,因为当时没用long long 导致最后测数据的时候wa了,大哭一场啊!!!

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+6;
struct node
{
int x,y;
};
node a[N];
int cmp1(node u,node v)
{
return u.x<v.x;
}
int cmp2(node u,node v)
{
if(u.y==v.y)return u.x<v.x;
return u.y<v.y;
}
/*
int cmp3(node u,node v)
{
if(u.x==v.x)
{
return u.y<v.y;
}
return u.x<v.x;
}*/
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp1);
long long num=1,ans=0;
for(int i=1;i<n;i++)
{
if(a[i].x==a[i-1].x)
{
num++;
}
else
{
long long s=(num-1)*num/2;
ans+=s;
num=1;
}
}
long long s=(num-1)*num/2;
ans+=s;
sort(a,a+n,cmp2);
num=1;
for(int i=1;i<n;i++)
{
if(a[i].y==a[i-1].y)
{
num++;
}
else
{
long long s=(num-1)*num/2;
ans+=s;
num=1;
}
}
s=num*(num-1)/2;
ans+=s;
num=1;
for(int i=1;i<n;i++)
{
if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y)
{
num++;
}
else
{
long long s=num*(num-1)/2;
ans-=s;
num=1;
}
}
s=num*(num-1)/2;
ans-=s; cout<<ans<<"\n"; return 0;
}

codeforces 650 C. Watchmen(数学公式)的更多相关文章

  1. Codeforces 650 D. Zip-line

    $ >Codeforces \space 650 D. Zip-line<$ 题目大意 : 有一个长度为 \(n\) 的序列 \(h\) ,\(m\) 次询问,每一次询问求如果把序列中第 ...

  2. CodeForces 651 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 650 D】Zip-line

    题意:给一个序列以及\(n\)个查询,每一个查询是问(假装)把第\(a_i\)个数改为\(b_i\)之后原序列的最长上升子序列的长度. 思路:线段树优化\(dp\). 肯定离线做啊. 首先我们考虑\( ...

  5. CodeForces 630Q Pyramids(数学公式)

    IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project ...

  6. 【Codeforces Round 650】Codeforces #334 (Div. 1)

    模拟CF650,ABC三题,RK90 Codeforces 650 A 思路:首先看式子 \(\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}=|x_i-x_j|+|y_i-y_j|\) ...

  7. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

  8. Watchmen CodeForces - 650A

    Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...

  9. (水题)Codeforces - 650A - Watchmen

    http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...

随机推荐

  1. #define的使用方法体会

    #define 创建一个宏,该宏是标识符或參数化标识符与标记字符串的关联. 在定义宏之后.编译器可用标记字符串替换源文件里标识符的每一个匹配项. 双击以所有折叠.">语法 #defin ...

  2. Linq系列(7)——表达式树之ExpressionVisitor

    大家好,由于今天项目升级,大家都在获最新代码,所以我又有时间在这里写点东西,跟大家分享. 在上一篇的文章中我介绍了一个dll,使大家在debug的时候可以可视化的看到ExpressionTree的Bo ...

  3. error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad Touch of exactly '120x120' pixels,in.pen format for ios versions >= 7.0

    error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad ...

  4. vue2 本地安装

  5. java.sql.SQLException: 无法转换为内部表示 -〉java 查询oracle数据库返回错误信息

    java.sql.SQLException: 无法转换为内部表示 Query: SELECT * FROM  nontheasttycoon Parameters: []    at org.apac ...

  6. Unity3d 摇杆奖励

    单个单元: publicclass RockerSingle : MonoBehaviour { // 枚举.类别 RockerType  rockerType; //是否有效,最上面的为无效,即为f ...

  7. OC常用函数及变量

    1.OC常用的的函数及变量 (1)算术函数 [算术函数] 函数名 说明 int rand() 随机数生成.(例)srand(time(nil)); //随机数初期化int val = rand()P; ...

  8. mydql练习答案

    .查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...

  9. centos6安装nginx最详细步骤

    第一步:在centos下面下载 nginx          wget http://nginx.org/download/nginx-1.2.9.tar.gz 解压 tar zxf nginx-1. ...

  10. linux c编程:Posix共享内存区

    Posix共享内存区:共享内存是最快的可用IPC形式.它允许多个不相关(无亲缘关系)的进程去访问同一部分逻辑内存.如果需要在两个进程之间传输数据,共享内存将是一种效率极高的解决方案.一旦这样的内存区映 ...