链接
https://www.luogu.org/problem/show?pid=2992#sub
题解
一道好题。
包含原点的三角形求起来貌似没什么捷径,复杂度会飚起。考虑不包含原点的三角形,我们枚举一个点,那么在它逆时针方向且小于180度的那些点中任意选两个构成的三角形(或线段)都是不过原点的。所以按极角排序,类似旋转卡壳的方法找那个最远点,当前点和最远的点(包含)中间夹着的那些点中选取两个的方案数就是答案,然后用C(n,3)减去这些数量。统计是O(N)的,排序还有一个NlogN,因此O(NlogN)是最后的复杂度。
代码
//容斥
#include <cstdio>
#include <algorithm>
#include <cmath>
#define eps 1e-8
#define maxn 100010
#define ll long long
using namespace std;
ll ans, N;
struct point
{
ll x, y;
double th;
}pt[maxn];
bool operator<(point p1, point p2){return p1.th<p2.th;}
ll operator*(point p1, point p2){return p1.x*p2.y-p2.x*p1.y;}
void input()
{
ll i;
scanf("%lld",&N);
for(i=1;i<=N;i++)
scanf("%lld%lld",&pt[i].x,&pt[i].y),pt[i].th=atan2(pt[i].y,pt[i].x);
sort(pt+1,pt+N+1);
}
void work()
{
ll i, j, t=0;
ans=N*(N-1)*(N-2)/6;
for(i=1,j=2;i<=N;i++,t--)
{
for(;pt[j]*pt[i]<0;j=j==N?1:j+1)t++;
ans-=t*(t-1)/2;
}
}
int main()
{
input();
work();
printf("%lld",ans);
return 0;
}