We need to parse problem: Two formulas: Doctor Manhattan distance = |xi - xj| + |yi - yj| (Manhattan distance). Daniel's distance = sqrt((xi - xj)^2 + (yi - yj)^2) (Euclidean distance). They are equal if and only if? Actually condition: |xi - xj| + |yi - yj| = sqrt((xi - xj)^2 + (yi - yj)^2). Square both sides? Let dx = |x_i - x_j|, dy = |y_i - y_j|. Then we need dx + dy = sqrt(dx^2 + dy^2). Square: (dx+dy)^2 = dx^2 + dy^2 => dx^2+2dx dy+dy^2 = dx^2+dy^2 => 2dx dy = 0 => dx=0 or dy=0. So condition holds iff either x_i = x_j or y_i = y_j (since absolute difference zero). So pairs where points share same x-coordinate or same y-coordinate. But careful: if both x and y are same, then both distances are zero, and condition holds, but also counted in both categories. We'll need to count pairs (i<j) such that x_i = x_j or y_i = y_j, and subtract double-counted pairs where both equal (i.e., identical points). So answer = sum over distinct x values: C(cnt_x,2) + sum over distinct y values: C(cnt_y,2) - sum over each point? Actually need to subtract pairs where both x and y equal, i.e., duplicate points. For each set of identical points (same (x,y)), the number of pairs among them satisfies both conditions, and they were counted twice (once in x-group, once in y-group). So we need to subtract for each distinct coordinate pair the number of pairs among them: C(cnt_{xy},2). So answer = sum_x C(cnt_x,2) + sum_y C(cnt_y,2) - sum_{each distinct (x,y)} C(cnt_{point},2).
Given n up to 200k, coordinates up to 1e9. Use maps to count frequencies.
We need to compute combinations: C(k,2) = k*(k-1)/2. Use long long.
Examples: first sample: points (1,1),(7,5),(1,5). Count x: 1 appears twice -> C(2,2)=1; 7 appears once ->0; sum_x=1. Count y: 1 appears once,5 appears twice -> C(2,2)=1; sum_y=1. Distinct points: (1,1):1, (7,5):1, (1,5):1 -> each C(1,2)=0, so subtract 0. Total=2. matches.
Second sample: points: (0,0), (0,1), (0,2), (-1,1), (0,1), (1,1). Let's compute: x frequencies: 0 appears? Points: indices: 1 (0,0),2 (0,1),3 (0,2),5 (0,1) -> that's four with x=0? Actually check: (0,0), (0,1), (0,2), (0,1) => yes 4 points with x=0; x=-1 appears once; x=1 appears once. So C(4,2)=6; others 0 => sum_x=6. y frequencies: y=0: (0,0) =>1; y=1: (0,1),( -1,1),(0,1),(1,1) => that's four? Actually points: (0,1) appears twice? Yes indices 2 and 5; plus (-1,1), (1,1) => total 4; y=2: (0,2) =>1. So C(4,2)=6, C(1,2)=0, C(1,2)=0 => sum_y=6. Distinct points: list: (0,0):1, (0,1):2, (0,2):1, (-1,1):1, (1,1):1. For each: C(1,2)=0 except (0,1): C(2,2)=1. So subtract 1. Total=6+6-1=11. matches.
So solution is straightforward.
Now implement:
- Read n.
- Use map<int, long long> cnt_x, cnt_y.
- Use map<pair<int,int>, long long> cnt_xy, or use a map with combined key (e.g., using string or tuple). Since coordinates up to 1e9, we can use unordered_map with hashed pair, but simpler: use map<pair<int,int>, long long>.
- For each point (x,y), increment cnt_x[x]++, cnt_y[y]++, cnt_xy[{x,y}]++.
- After reading all, iterate over cnt_x, sum += cnt*(cnt-1)/2.
- Similarly for cnt_y.
- Iterate over cnt_xy, subtract cnt*(cnt-1)/2.
- Print result.
Be careful with 64-bit: n up to 200k, max pairs ~ n*(n-1)/2 ≈ 2e5*2e5/2 = 2e10, fits in 64-bit (around 2e10 < 2^63). So use long long.
Complexity O(n log n) due to maps, acceptable.
Alternative: use unordered_map for O(1) average. But given n=200k, map is fine.
Edge cases: n=1 => answer 0.
Implementation in C++:
#include
#include
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
map<int, ll> cnt_x, cnt_y;
map<pair<int,int>, ll> cnt_xy;
for(int i=0;i<n;++i){
int x,y;
cin >> x >> y;
cnt_x[x]++;
cnt_y[y]++;
cnt_xy[{x,y}]++;
}
ll ans = 0;
for(auto &p : cnt_x){
ll c = p.second;
ans += c*(c-1)/2;
}
for(auto &p : cnt_y){
ll c = p.second;
ans += c*(c-1)/2;
}
for(auto &p : cnt_xy){
ll c = p.second;
ans -= c*(c-1)/2;
}
cout << ans << endl;
return 0;
}
That's it.
But problem statement says: "Some positions may coincide." So duplicate points allowed.
Double-check condition: We derived dx*dy = 0 => either dx=0 or dy=0. But is there any other scenario? If dx and dy are both non-zero, the squared equation yields 2dxdy=0, impossible. So yes.
Thus answer.
Now produce final answer. ```cpp
#include
#include
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
map<int, ll> cntX, cntY;
map<pair<int, int>, ll> cntXY;
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
cntX[x]++;
cntY[y]++;
cntXY[{x, y}]++;
}
ll ans = 0;
for (auto& p : cntX) {
ll c = p.second;
ans += c * (c - 1) / 2;
}
for (auto& p : cntY) {
ll c = p.second;
ans += c * (c - 1) / 2;
}
for (auto& p : cntXY) {
ll c = p.second;
ans -= c * (c - 1) / 2;
}
cout << ans << endl;
return 0;
}