题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631
数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近点对就可以了。。。
1 //STATUS:G++_AC_10390MS_23804KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,102400000") 25 //using namespace __gnu_cxx; 26 //define 27 #define pii pair<int,int> 28 #define mem(a,b) memset(a,b,sizeof(a)) 29 #define lson l,mid,rt<<1 30 #define rson mid+1,r,rt<<1|1 31 #define PI acos(-1.0) 32 //typedef 33 typedef __int64 LL; 34 typedef unsigned __int64 ULL; 35 //const 36 const int N=500010; 37 const int INF=0x3f3f3f3f; 38 const int MOD=10007,STA=8000010; 39 const LL LNF=1LL<<55; 40 const double EPS=1e-8; 41 const double OO=1e15; 42 const int dx[4]={-1,0,1,0}; 43 const int dy[4]={0,1,0,-1}; 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 45 //Daily Use ... 46 inline int sign(double x){return (x>EPS)-(x<-EPS);} 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 50 template<class T> inline T Min(T a,T b){return a<b?a:b;} 51 template<class T> inline T Max(T a,T b){return a>b?a:b;} 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 56 //End 57 58 struct Node{ 59 LL x,y; 60 LL id,index; 61 Node(){} 62 Node(LL _x,LL _y,LL _index):x(_x),y(_y),index(_index){} 63 }p[N],nod[N],temp[N]; 64 65 int n; 66 67 LL dist(Node &a,Node &b) 68 { 69 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); 70 } 71 72 int cmpxy(Node a,Node b) 73 { 74 return a.x!=b.x?a.x<b.x:a.y<b.y; 75 } 76 77 int cmpy(Node a,Node b) 78 { 79 return a.y<b.y; 80 } 81 82 pii Closest_Pair(int l,int r) 83 { 84 if(l==r || l+1==r)return pii(l,r); 85 LL d,d1,d2; 86 int i,j,k,mid=(l+r)/2; 87 pii pn1=Closest_Pair(l,mid); 88 pii pn2=Closest_Pair(mid+1,r); 89 d1=(pn1.first==pn1.second?LNF:dist(nod[pn1.first],nod[pn1.second])); 90 d2=(pn2.first==pn2.second?LNF:dist(nod[pn2.first],nod[pn2.second])); 91 pii ret; 92 d=Min(d1,d2); 93 ret=d1<d2?pn1:pn2; 94 for(i=l,k=0;i<=r;i++){ 95 if((nod[mid].x-nod[i].x)*(nod[mid].x-nod[i].x)<=d){ 96 temp[k++]=nod[i]; 97 } 98 } 99 sort(temp,temp+k,cmpy); 100 for(i=0;i<k;i++){ 101 for(j=i+1;j<k && (temp[j].y-temp[i].y)*(temp[j].y-temp[i].y)<d;j++){ 102 if(dist(temp[i],temp[j])<d){ 103 d=dist(temp[i],temp[j]); 104 ret=make_pair(temp[i].id,temp[j].id); 105 } 106 } 107 } 108 109 return ret; 110 } 111 112 void Init() 113 { 114 int i; 115 LL x,y,Ax,Bx,Cx,Ay,By,Cy; 116 cin>>n>>Ax>>Bx>>Cx>>Ay>>By>>Cy; 117 x=y=0; 118 for(i=0;i<n;i++){ 119 x=(x*Ax+Bx)%Cx; 120 y=(y*Ay+By)%Cy; 121 p[i]=Node(x,y,i); 122 } 123 } 124 125 int main(){ 126 // freopen("in.txt","r",stdin); 127 int T,i,j,k; 128 LL ans,hig; 129 scanf("%d",&T); 130 while(T--) 131 { 132 Init(); 133 134 int end=n; 135 pii t; 136 ans=0; 137 while(end>0){ 138 for(i=0;i<end;i++)nod[i]=p[i]; 139 sort(nod,nod+end,cmpxy); 140 for(i=0;i<end;i++)nod[i].id=i; 141 t=Closest_Pair(0,end-1); 142 hig=Max(nod[t.first].index,nod[t.second].index); 143 ans+=(end-hig)*dist(nod[t.first],nod[t.second]); 144 end=hig; 145 } 146 cout<<ans<<endl; 147 } 148 return 0; 149 }