题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离
也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可。
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <cstring> 10 #include <math.h> 11 #include <stdlib.h> 12 #include <time.h> 13 #include <stack> 14 #define clc(a,b) memset(a,b,sizeof(a)) 15 #define LL long long 16 using namespace std; 17 const int maxn=200020; 18 const int inf=0x3f3f3f3f; 19 struct node{ 20 LL x,y; 21 }a[maxn]; 22 bool cmp1(node a,node b){ 23 if(a.x==b.x) return a.y<b.y; 24 return a.x<b.x; 25 } 26 27 bool cmp2(node a,node b){ 28 if(a.y==b.y) return a.x<b.x; 29 return a.y<b.y; 30 } 31 32 int n; 33 // int a[maxn],b[maxn]; 34 int main(){ 35 scanf("%d",&n); 36 LL sum=0; 37 for(int i=0;i<n;i++){ 38 scanf("%I64d%I64d",&a[i].x,&a[i].y); 39 } 40 sort(a,a+n,cmp1); 41 LL sum1=0; 42 for(int i=0;i<n;i++){ 43 int j=i; 44 sum1=0; 45 while(j<n&&a[i].x==a[j].x){ 46 sum1++; 47 j++; 48 } 49 i=j-1; 50 sum+=sum1*(sum1-1)/2; 51 } 52 sort(a,a+n,cmp2); 53 sum1=0; 54 for(int i=0;i<n;i++){ 55 int j=i; 56 sum1=0; 57 while(j<n&&a[i].y==a[j].y){ 58 sum1++; 59 j++; 60 } 61 i=j-1; 62 sum+=sum1*(sum1-1)/2; 63 } 64 sum1=0; 65 for(int i=0;i<n;i++){ 66 int j=i; 67 sum1=0; 68 while(j<n&&a[i].y==a[j].y&&a[j].x==a[i].x){ 69 sum1++; 70 j++; 71 } 72 i=j-1; 73 sum-=sum1*(sum1-1)/2; 74 } 75 printf("%I64d\n",sum); 76 return 0; 77 }
时间: 2024-10-10 15:27:41