hdu4631(set与二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631

Sad Love Story

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


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 p i(x i, y i).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 x 1, x 2, ..., x n,we let x 0 = 0,and give you 3 parameters:A,B,C. Then x i = (x i-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 A x B x C x A y B y C y.
A x,B x,C x is the given parameters for x 1, ..., x n.
A y,B y,C y is the given parameters for y 1, ..., y n.
T <= 10. 
n <= 5 * 10 5.
10 4 <= A,B,C <= 10 6.
 

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

Sample Input
  
  
2 5 765934 377744 216263 391530 669701 475509 5 349753 887257 417257 158120 699712 268352
 

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

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4822  4821  4820  4819  4818 
 

不难,不过如果不进行二分硬算的话,肯定会超。所以我们用multiset来优化,用二分查找,找到插入点的位置p,然后从p向右算,再从p向左算。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define LL long long 
using namespace std;
const int MAXN=600000;
LL Ax,Bx,Cx,Ay,By,Cy,ans,n;
class Point
{
public:
	LL x,y;
	bool operator < (const Point &rhs) const 
	{
		return x<rhs.x;
	}
};
Point p[MAXN];
int main()
{
	int T;
	while(cin>>T)
	{
		while(T--)
		{
			cin>>n>>Ax>>Bx>>Cx>>Ay>>By>>Cy;
			p[0].x=0;
			p[0].y=0;
			multiset<Point> S;
			S.clear();
			for(int i=1;i<=n;i++)
			{
				p[i].x=(p[i-1].x*Ax+Bx)%Cx;
				p[i].y=(p[i-1].y*Ay+By)%Cy;
			}
			LL mindis;
			mindis=((LL) 1<<62);
			S.insert(p[1]);
			ans=0;
			for(int i=2;i<=n;i++)
			{
				multiset<Point>::iterator pp=S.lower_bound(p[i]),iter;
				for(iter=pp;iter!=S.begin();)
				{
					iter--;
					Point t=*iter;
					LL a=t.x-p[i].x;
					a*=a;
					if(a>=mindis)
						break;
					LL b=t.y-p[i].y;
					b*=b;
					mindis=min(mindis,a+b);
				}
				for(;pp!=S.end();pp++)
				{
					Point t=*pp;
					LL a=t.x-p[i].x;
					a*=a;
					if(a>=mindis)
						break;
					LL b=t.y-p[i].y;
					b*=b;
					mindis=min(mindis,a+b);
				}
				S.insert(p[i]);
				ans+=mindis;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值