2411. Triangles
(File IO): input: triangles.in output: triangles.out
Time limit: 1000 ms Space limit: 262144 KB Specific limit
Goto ProblemSet
Title description
Farmer John wants to build a triangular pasture for his cows. There are N (3≤N≤10 ^ 5) fence posts located at different points (X1, Y1) ... (XN, YN) on the two-dimensional plane of the farm. He can choose three points to form a triangle pasture, as long as one side of the triangle is parallel to the x-axis and the other side is parallel to the y-axis.
What is the sum of all possible pasture areas that FJ can make up?
Input
The first line contains N.
Each of the following N rows contains two integers Xi and Yi, both in the range −10 4… 10 4, describing the position of a fence post.
Output
Since the sum of areas is not necessarily an integer and may be very large, the sum of the output areasdoubleThe remainder modulo 10 ^ 9 + 7.
Sample input
4
0 0
0 1
1 0
1 2
Sample output
3
The data range limits
test points 1-2 to N = 200.
Test points 3-4 satisfy N≤5000.
Test points 5-10 have no additional restrictions.
Tips:
Fence stakes (0,0), (1,0), and (1,2) form a triangle with area 1, and (0,0), (1,0), and (0,1) form a triangle A triangle with an area of 0.5. So the answer is 2 * (1 + 0.5) = 3.
Method 1:
We enumerate one point (i) at a time, and then find sumx [i] (the sum of the distance between point i and point i and point i) and sumy [i] (same point as i The distance between the coordinate point and point i); and then find the total area of the triangle with point i as the apex (sumx [i] * sumy [i]);
sumx[p[i].num]=sumx[p[i-1].num]+(2*k-2-tot)*d;
Points with the same y coordinate can be calculated from the distance and extrapolation of a point, so that there is no need to repeat calculations, thereby reducing the time complexity of the program
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int Mod=1e9+7;
const int N=1e5+10;
struct node
{
long long x,y,num;
} p[N];
long long n,sumx[N],sumy[N];
bool cmp1(node xx,node yy) {return (xx.y==yy.y)?xx.x<yy.x:xx.y<yy.y;}
bool cmp2(node xx,node yy) {return (xx.x==yy.x)?xx.y<yy.y:xx.x<yy.x;}
int main()
{
fre(triangles);
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld%lld",&p[i].x,&p[i].y),p[i].num=i;
sort(p+1,p+1+n,cmp1);
int t,tot,k;
p[0].x=p[0].y=-10000000;
for(int i=1;i<=n;i++)
{
if(p[i].y!=p[i-1].y) t=i,tot=0,k=1;
else
{
k++;
int d=p[i].x-p[i-1].x;
sumx[p[i].num]=sumx[p[i-1].num]+(2*k-2-tot)*d;
continue;
}
for(int j=t;j<=n;j++)
{
if(p[i].y==p[j].y) sumx[p[i].num]+=p[j].x-p[i].x,tot++;
else break;
}
}
sort(p+1,p+1+n,cmp2);
t=1;
for(int i=1;i<=n;i++)
{
if(p[i].x!=p[i-1].x) t=i,tot=0,k=1;
else
{
k++;
int d=p[i].y-p[i-1].y;
sumy[p[i].num]=sumy[p[i-1].num]+(2*k-2-tot)*d;
continue;
}
for(int j=t;j<=n;j++)
{
if(p[i].x==p[j].x) sumy[p[i].num]+=p[j].y-p[i].y,tot++;
else break;
}
}
long long ans=0;
for(int i=1;i<=n;i++) ans+=(sumx[i]*sumy[i])%Mod,ans%=Mod;
printf("%lld",ans);
return 0;
}
Method 2: It
is different from Method 1: it only divides the total area of a triangle into four quadrants. Of course, the distance and formula will be different.
sumx[xx]=(sumx[xx]+abs(sx[xx]-yy)*(tx[xx]-1));
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=100010;
const int Mod=1e9+7;
struct node
{
long long x,y;
} p[N];
int n,ans;
bool cmp1(node xx,node yy) {return (xx.x!=yy.x)?xx.x<yy.x:xx.y<yy.y;}
bool cmp2(node xx,node yy) {return (xx.x!=yy.x)?xx.x<yy.x:xx.y>yy.y;}
bool cmp3(node xx,node yy) {return (xx.x!=yy.x)?xx.x>yy.x:xx.y<yy.y;}
bool cmp4(node xx,node yy) {return (xx.x!=yy.x)?xx.x>yy.x:xx.y>yy.y;}
void js()
{
long long tx[20010]={},ty[20010]={};
long long sumx[20010]={},sumy[20010]={};
long long sx[20010]={},sy[20010]={};
for(int i=1;i<=n;i++)
{
int xx=p[i].x,yy=p[i].y;
tx[xx]++;
sumx[xx]=(sumx[xx]+abs(sx[xx]-yy)*(tx[xx]-1))%Mod;
sx[xx]=yy;
ty[yy]++;
sumy[yy]=(sumy[yy]+abs(sy[yy]-xx)*(ty[yy]-1))%Mod;
sy[yy]=xx;
ans=(ans+sumx[xx]*sumy[yy])%Mod;
}
}
int main()
{
fre(triangles);
cin>>n;
for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y,p[i].x+=10000,p[i].y+=10000;
sort(p+1,p+1+n,cmp1); js();
sort(p+1,p+1+n,cmp2); js();
sort(p+1,p+1+n,cmp3); js();
sort(p+1,p+1+n,cmp4); js();
cout<<ans<<endl;
return 0;
}