Problem Description
Problem Analysis
From the problem statement, it can be seen that the points satisfying the condition
must have the same x or the same y
(this can be obtained by simplifying the equation)
The tricky part is that there are
identical points
so duplicates need to be removed during calculation
I worked on this problem on and off for several hours
so my train of thought was a bit messy
and by the end I got a little panicked
which made me forget some small tricks
and the more panicked I got, the more mistakes I made
luckily I finally managed to push it through
I kept thinking about avoiding double counting
but in fact, with a different approach
I could also think of adding everything first
and then removing the duplicates
Finally, note that
the data type should be __int64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| #include<stdio.h>
#include<algorithm>
using namespace std;
struct xy {
int x,y;
} node[200200];
bool cmpx(xy A,xy B) {
if(A.x==B.x)
return A.y<B.y;
else
return A.x<B.x;
}
bool cmpy(xy A,xy B) {
if(A.y==B.y)
return A.x<B.x;
else
return A.y<B.y;
}
int main() {
int n;
while(scanf("%d",&n)!=EOF) {
for(int i=0; i<n; i++) {
scanf("%d %d",&node[i].x,&node[i].y);
}
__int64 res=0;
sort(node,node+n,cmpx);
__int64 t=1,ct=1;
for(int i=1; i<n; i++) {
if(node[i].x==node[i-1].x) {
t++;
if(node[i].y==node[i-1].y)
ct++;
else {
res-=ct*(ct-1)/2;
ct=1;
}
} else {
res+=t*(t-1)/2;
res-=ct*(ct-1)/2;
ct=1;
t=1;
}
}
if (ct>1) {
res-=ct*(ct-1)/2;
}
if (t>1) {
res+=t*(t-1)/2;
}
sort(node,node+n,cmpy);
t=1;
for(int i=1; i<n; i++) {
if(node[i].y==node[i-1].y) {
t++;
} else {
res+=t*(t-1)/2;
t=1;
}
}
if(t>1)
res+=t*(t-1)/2;
printf("%I64d\n",res);
}
return 0;
}
|
Problem address:【CodeForces】[651C]Watchmen