hdu 4631 Sad Love Story (STL+multiset)

来源:互联网 发布:网络电话卡有哪些 编辑:程序博客网 时间:2024/05/22 17:28

Sad Love Story

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1710    Accepted Submission(s): 548


Problem Description
There's a really sad story.It could be about love or about money.But love will vanish and money will be corroded.These points will last forever.So this time it is about points on a plane.
We have a plane that has no points at the start.
And at the time i,we add point pi(xi, yi).There is n points in total.
Every time after we add a point,we should output the square of the distance between the closest pair on the plane if there's more than one point on the plane.
As there is still some love in the problem setter's heart.The data of this problem is randomly generated.
To generate a sequence x1, x2, ..., xn,we let x0 = 0,and give you 3 parameters:A,B,C. Then xi = (xi-1 * A + B) mod C.
The parameters are chosen randomly.
To avoid large output,you simply need output the sum of all answer in one line.
 

Input
The first line contains integer T.denoting the number of the test cases.
Then each T line contains 7 integers:n Ax Bx Cx Ay By Cy.
Ax,Bx,Cx is the given parameters for x1, ..., xn.
Ay,By,Cy is the given parameters for y1, ..., yn.
T <= 10.
n <= 5 * 105.
104 <= A,B,C <= 106.
 

Output
For each test cases,print the answer in a line.
 

Sample Input
25 765934 377744 216263 391530 669701 4755095 349753 887257 417257 158120 699712 268352
 

Sample Output
823750312549959926940
Hint
If there are two points coincide,then the distance between the closest pair is simply 0.
 

题意: 给你坐标的计算公式xi=(xi-1*A+B)%C(y计算相同,但A、B、C值的不同),然后可以得

到n个坐标,在平面上添加坐标,每添加一个坐标则计算平面上2坐标间的最小值,然后要求

输出所有最小值的和。

思路:按照题目要求,按1-n的顺序添加点。在已有的点集中按X坐标从小到大排序,每增加一

个点,找到大于等于它的位置t,分为两部分,然后从右递增计算要增加的点与点集中的点的距

离,若>=Min,则退出,然后再从t-1处从右往左递减计算要增加的点与点集中所有点的距离的最

小值。

 

#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <set>#define LL long longusing namespace std;const LL inf=((LL)1<<62);const int maxn=500010;struct node{    LL x,y;} a[maxn],A,B,C;int n,T;bool operator < (node p,node q){    return p.x<q.x;}void input(){    scanf("%d",&n);    scanf("%I64d %I64d %I64d",&A.x,&B.x,&C.x);    scanf("%I64d %I64d %I64d",&A.y,&B.y,&C.y);    a[0].x=a[0].y=0;    for(int i=1; i<=n; i++)    {        a[i].x=(a[i-1].x*A.x+B.x)%C.x;        a[i].y=(a[i-1].y*A.y+B.y)%C.y;    }}void solve(){    multiset <node> st;    multiset <node>::iterator it,t;    st.insert(a[1]);    LL ans=0,Min=inf;    for(int i=2; i<=n; i++)    {        node tmp=a[i];        t=st.lower_bound(tmp);        for(it=t;it!=st.begin();)        {            it--;            LL xx=it->x-tmp.x;            xx=xx*xx;            if(xx>=Min)   break;            Min=min(Min,xx+(it->y-tmp.y)*(it->y-tmp.y));        }        for(it=t;it!=st.end();it++)        {            LL xx=it->x-tmp.x;            xx=xx*xx;            if(xx>=Min)   break;            Min=min(Min,xx+(it->y-tmp.y)*(it->y-tmp.y));        }        st.insert(a[i]);        ans+=Min;    }    printf("%I64d\n",ans);}int main(){    scanf("%d",&T);    while(T--)    {        input();        solve();    }    return 0;}


 

0 0