bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形
滑动窗口
将不是黄金三角形的减去,时间复杂度O(nlog2n)+O(n)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 struct xl 34 { 35 double x,y,rad; 36 xl(double x=0,double y=0):x(x),y(y){} 37 void in(){douin(x),douin(y);rad=atan2(y,x);} 38 bool op < (const xl &a)const {return rad<a.rad;} 39 xl op + (const xl &a)const {return xl(x+a.x,y+a.y);} 40 xl op - (const xl &a)const {return xl(x-a.x,y-a.y);} 41 xl op * (const double &a)const {return xl(x*a,y*a);} 42 xl op / (const double &a)const {return xl(x/a,y/a);} 43 }; 44 double X_(const xl &a,const xl &b) 45 { 46 return a.x*b.y-a.y*b.x; 47 } 48 xl a[200010]; 49 LL ans; 50 int n; 51 int main() 52 { 53 inin(n);re(i,1,n)a[i].in(); 54 sort(a+1,a+n+1);re(i,1,n)a[i+n]=a[i]; 55 int r=1; 56 re(l,1,n) 57 { 58 while(r<l+n&&X_(a[l],a[r])>=0) 59 r++; 60 int shu=r-l; 61 if(shu>2)ans+=(LL)(shu-2)*(shu-1)/2; 62 } 63 printf("%lld",(LL)n*(n-1)*(n-2)/6-ans); 64 return 0; 65 }