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.
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.
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.
3
1 1
7 5
1 5
2
6
0 0
0 1
0 2
-1 1
0 1
1 1
11
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.
题意: n个点 分别给出x,y坐标
两种两点的计算方式 |xi - xj| + |yi - yj|. .
问 有多少种点的组合使得 两种两点的计算方式的结果相等 欧几里得距离等于曼哈顿距离
题解: 等式两边平方 整理移项之后可以发现 两点的x坐标相等或者y坐标相等情况下两种计算方式结果相等
用了 multiset+pair
其中最关键的处理是 当x y都对应相等情况下 去重处理
用的是pair
x相等的+y相等的-xy相等的
1 #include<bits/stdc++.h> 2 #include<iostream> 3 #include<cstdio> 4 #define ll __int64 5 using namespace std; 6 int n; 7 multiset<int>s1; 8 multiset<int>s2; 9 pair<int,int>gg; 10 multiset<pair<int,int > >ggg; 11 int a[200005],b[200005]; 12 ll jishu=0; 13 int main() 14 { 15 scanf("%d",&n); 16 for(int i=1;i<=n;i++) 17 { 18 scanf("%d %d",&a[i],&b[i]); 19 gg.first=a[i]; 20 gg.second=b[i]; 21 ggg.insert(gg); 22 s1.insert(a[i]); 23 s2.insert(b[i]); 24 } 25 for(int i=1;i<=n;i++) 26 { 27 ll exm=s1.count (a[i]); 28 jishu=jishu+exm*(exm-1)/2; 29 s1.erase(a[i]); 30 } 31 for(int i=1;i<=n;i++) 32 { 33 ll exm=s2.count(b[i]); 34 jishu=jishu+exm*(exm-1)/2; 35 s2.erase(b[i]); 36 } 37 //printf("%I64d ",jishu); 38 for(int i=1;i<=n;i++) 39 { 40 gg.first=a[i]; 41 gg.second=b[i]; 42 ll exm=ggg.count(gg); 43 jishu=jishu-exm*(exm-1)/2; 44 ggg.erase(gg); 45 } 46 printf("%I64d ",jishu); 47 return 0; 48 }