HDU4631Sad Love Story
这道题是用multiset直接维护就行了(可是我根本不会multiset)
用一些剪枝就能跑出来
还有
不要爆int
#include <set> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const long long inf=1ll<<60; inline int read(){ int x=0,f=1,ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct point{ long long x,y; bool operator < (const point &b)const{ return x<b.x; } }; multiset<point> s; int main(){ int T=read(); while(T--){ int n=read(),Ax=read(),Bx=read(),Cx=read(),Ay=read(),By=read(),Cy=read(); s.clear();point a=(point){0,0}; long long ans=0,minn=inf; for(int i=1;i<=n;i++){ a.x=(a.x*Ax+Bx)%Cx; a.y=(a.y*Ay+By)%Cy; // cout<<a.x<<"\t"<<a.y<<endl; if(i!=1){ multiset<point>::iterator p=s.lower_bound(a),it; for(it=p;it!=s.end();it++){ long long dx=a.x-it->x; dx*=dx; if(dx>=minn) break; long long dy=a.y-it->y; dy*=dy; minn=min(minn,dx+dy); } for(it=p;it!=s.begin();){ it--; long long dx=a.x-it->x; dx*=dx; if(dx>=minn) break; long long dy=a.y-it->y; dy*=dy; minn=min(minn,dx+dy); } ans+=minn; // cout<<minn<<endl; } s.insert(a); } printf("%lld\n",ans); } return 0; }