time limit per test :3 seconds
memory limit per test :256 megabytes
This problem is same as the previous one, but has larger constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates
. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer
— the number of electric poles.
Each of the following
lines contains two integers
— the coordinates of the poles.
It is guaranteed that all of these
points are distinct.
Output
Print a single integer — the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
Copy
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
In the second example:
Note that the three poles and are connected by a single wire.
In the third example:
题意:
给定
个不同的点,这
个点两两相连会形成一条直线,重合的直线算作同一条,询问这些直线中,两两相交的直线的对数是多少?
题解:
将所有直线转换成
或者
的形式, 然后斜率不同的直线肯定是可以直接相交的。
直接计算即可。
map/set会卡double的精度,建议手写个分数类。
#include<bits/stdc++.h>
#define LiangJiaJun main
#define INF make_pair(1LL,0LL)
#define eps 1e-10
#define ll long long
#define pa pair<ll,ll>
using namespace std;
pa work(ll x,ll y){
if(y==0)return make_pair(1LL,0LL);
if(x==0)return make_pair(0LL,1LL);
ll a=abs(x),b=abs(y);
ll d=__gcd(a,b);
ll f=((x*y<0)?-1:1);
return make_pair(f*a/d,b/d);
}
struct point{
ll x,y;
pa ck(){
return work(y,x);
}
}p[1004];
int n;
struct line{
point s,e;
line(point S,point E){s=S;e=E;}
};
point operator-(point A,point B){
point T;
T.x=A.x-B.x;
T.y=A.y-B.y;
return T;
}
set<pair<pa,pa> >la;
map<pa,int>gs;
map<pa,int>::iterator it;
void ins(line a){
pa k=(a.e-a.s).ck();
pa b;
if(k==INF){
b=make_pair(a.e.x,1LL);
}
else{
b=work(a.e.y*k.second-a.e.x*k.first,k.second);
}
int lp=la.size();
la.insert(make_pair(k,b));
if(lp!=la.size())gs[k]++;
}
int LiangJiaJun(){
la.clear();
gs.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld%lld",&p[i].x,&p[i].y);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
ins(line(p[i],p[j]));
}
}
ll ans=0;
ll sum=la.size();
for(it=gs.begin();it!=gs.end();it++){
ll gp=(it->second);
ans+=(sum-gp)*gp;
}
printf("%lld\n",ans/2);
return 0;
}