CodeForces - 650A Watchmen (握手定理)

来源:互联网 发布:妄想代理人知乎 编辑:程序博客网 时间:2024/05/21 19:13

题意:

给出 N 个坐标点,求有多少对点的坐标满足|xi - xj| + |yi - yj|=.

思路:

推导后发现 当两点的一个坐标相同时满足条件。

     所以求出每种 X 坐标和每种 Y 坐标的重复次数,利用握手定理。得满足题意的点对数为

     其中 q 为 X 坐标的种类, X[i] 为每种 X 坐标重复的个数,p 为 y 坐标的种类, Y[i] 为每种 Y 坐标重复的个数。

     但当两点相同时,会出现重复计算的情况,又由握手定理得

     ANS =


     其中 w 为重复点的种类, Z[i] 为每种点的重复次数

代码:

#include <bits/stdc++.h>using namespace std;const int MAXN=2e5+100;long long n,x,y;long long ans;map <long long,long long> cx,cy;map <pair<long long,long long>,long long >cp;pair <long,long> sav[MAXN];int main(){    ios::sync_with_stdio(false);    while(cin>>n){        ans=0;        cx.clear();cy.clear();        cp.clear();        for(int i=0;i<n;i++){            cin>>x>>y;            sav[i]=make_pair(x,y);            cx[x]++;            cy[y]++;            cp[sav[i]]++;        }        for(int i=0;i<n;i++){            long long temp=cp[sav[i]];            if(temp){                ans+=cx[sav[i].first]*(cx[sav[i].first]-1)/2;                ans+=cy[sav[i].second]*(cy[sav[i].second]-1)/2;                ans-=temp*(temp-1)/2;                cp[sav[i]]=0;                cx[sav[i].first]=0;                cy[sav[i].second]=0;            }        }        cout<<ans<<endl;    }}

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, thei-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.

Example
Input
31 17 51 5
Output
2
Input
60 00 10 2-1 10 11 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.


0 0
原创粉丝点击