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. 转载 OS js oc相互调用(JavaScriptCore) ---js调用iOS ---js里面直接调用方法

    OS js oc相互调用(JavaScriptCore)   接着上节我们讲到的iOS调用js 下来我们使用js调用iOS js调用iOS分两种情况 一,js里面直接调用方法 二,js里面通过对象调用 ...

  2. C语言基础知识【简介】

    C 简介1.关于 CC 语言是为了编写 UNIX 操作系统而被发明的.C 语言是以 B 语言为基础的,B 语言大概是在 1970 年被引进的.C 语言标准是于 1988 年由美国国家标准协会(ANSI ...

  3. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

  4. OKR与KPI管理的区别与联系

    OKR是一种新兴的管理体系,最近几年被引进中国.由于在IT.互联网.金融.游戏等知识密集型企业中有着显著的效果,得到中国企业的认可. OKR是英文Objectives & Key Result ...

  5. SDOI2012 Round1 day2 象棋(chess)解题报告

    本题的难点是“移动过程中不能出现多颗棋子同时在某一格的情况”. 事实上,可以忽略此条件,因为棋子是相同的,我们可以用合法的等效方案替代一棋子越过另一棋子的情况:A.B.C三格,A能在一步走到B,B也能 ...

  6. Python小练习(持续更新....)

    最近一直在学习python,这些小练习有些是书上的,有些是别人博客上的! # 1.题目1# 给一个字符串,统计其中的数字.字母和其他类型字符的个数:# 比如输入“124mid-=”,输出:数字=3,字 ...

  7. 关于Future

    1 为什么需要Callable和Future Runnable没有返回值,也不抛异常,这样主线程不能知道子线程的执行结果. 为了解决这个问题就有了Callable和Future.Callable提供的 ...

  8. 在普通网页中如何调用html5+的plus对象

    <script> //用法: HTML5+的plus对象,必须由click事件触发后,才能在普通网页中使用.所以在没有click的情况下,调用本文件可以解决问题! //在代码中使用: pl ...

  9. jQuery-Ajax-Timeout属性不生效的问题

    async必须设置为async:ture,timeout才生效: 如果设置为async:false,则锁住浏览器,禁止一切操作,直到请求有返回结果.

  10. 【leetcode刷题笔记】Insertion Sort List

    Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻 ...