# BZOJ1914: [Usaco2010 OPen]Triangle Counting 数三角形

n<=100000个点给坐标，保证没有两点连线过原点，求选三个点构成包含原点的三角形的个数。

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<math.h>
5 //#include<iostream>
6 using namespace std;
7
8 int n;
9 #define maxn 100011
10 struct Point
11 {
12     int area,x,y;double t;
13     bool operator < (const Point &b) const
14     {return area<b.area || (area==b.area && t>b.t);}
15 }p[maxn];
16 #define LL long long
17 int x,y;
18 const int inf=0x3f3f3f3f;
19 int main()
20 {
21     scanf("%d",&n);
22     for (int i=1;i<=n;i++)
23     {
24         scanf("%d%d",&x,&y);
25         p[i].x=x;p[i].y=y;
26         if (p[i].x) p[i].t=(double)y/x;else p[i].t=-inf;
27         if (x>0 && y>=0) p[i].area=1;
28         else if (x>=0 && y<0) p[i].area=2;
29         else if (x<0 && y<=0) p[i].area=3;
30         else p[i].area=4;
31     }
32     sort(p+1,p+1+n);
33     LL ans=1ll*n*(n-1)*(n-2)/6,now=1;
34     int tmp=2;
35     for (int i=1;i<=n;i++)
36     {
37         now--;
38         while (1ll*p[tmp].y*p[i].x<1ll*p[tmp].x*p[i].y) now++,tmp++,tmp-=tmp>n?n:0;
39         ans-=now*(now-1)/2;
40     }
41     printf("%lld\n",ans);
42     return 0;
43 }
View Code

posted @ 2017-08-31 08:34  Blue233333  阅读(139)  评论(0编辑  收藏  举报