QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#383790#5463. Range Closest Pair of Points Querywsc2008WA 101ms70692kbC++142.6kb2024-04-09 17:36:012024-04-09 17:36:01

Judging History

你现在查看的是最新测评结果

  • [2024-04-09 17:36:01]
  • 评测
  • 测评结果:WA
  • 用时:101ms
  • 内存:70692kb
  • [2024-04-09 17:36:01]
  • 提交

answer

#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
inline void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
const ll N=2.5e5+9,INF=(1ll<<60ll);
ll n,q,S,X[N],Y[N],blk[N],val[N],bel[N],ans[N],id[N];
vector<pii>vq[N];
vector<ll>to[N];
unordered_map<ll,pii>mp;
ll Dis(ll i,ll j){
	return (X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j]);
}
ll Id(ll i,ll j){
	return (i<<30ll)+j;
}
void Upd(ll x,ll y){
	blk[bel[x]]=min(blk[bel[x]],y);
	val[x]=min(val[x],y);
}
ll Query(ll p){
	ll res=INF;
	for(ll i=p;bel[i]==bel[p];++i)res=min(res,val[i]);
	rep(i,bel[p]+1,bel[n])res=min(res,blk[i]);
	return res;
}
bool Med;
int main(){
	cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
	n=read(),q=read(),S=sqrt(n);
	rep(i,1,n)X[i]=read(),Y[i]=read();
	rep(i,1,q){
		ll l=read(),r=read();
		vq[r].emplace_back(make_pair(l,i));
		ans[i]=INF;
	}
	rep(i,1,n)bel[i]=(i-1)/S+1;
	rep(d,0,27){
		mp.clear();
		rep(i,1,n)id[i]=i;
		sort(id+1,id+n+1,[&](ll i,ll j){
			return (X[i]>>d)==(X[j]>>d)?(X[i]>>d)<(X[j]>>d):(Y[i]>>d)<(Y[j]>>d);
		});
		ll l=1;
		while(l<=n){
			ll r=l;
			while(r<n&&(X[id[l]]>>d)==(X[id[r+1]]>>d)&&(Y[id[l]]>>d)==(Y[id[r+1]]>>d))r++;
			sort(id+l,id+r+1);
			mp[Id(X[id[l]]>>d,Y[id[l]]>>d)]=make_pair(l,r);
			rep(i,l,r){
				rep(j,i+1,min(r,i+3))to[id[j]].push_back(id[i]);
			}
			l=r+1;
		}
		l=1;
		while(l<=n){
			ll r=l,px=(X[id[l]]>>d),py=(Y[id[l]]>>d);
			while(r<n&&px==(X[id[r+1]]>>d)&&py==(Y[id[r+1]]>>d))r++;
			vector<pii>vdir={{-1,1},{0,1},{1,1},{1,0}};
			for(pii dir:vdir){
				ll dx=dir.first,dy=dir.second;
				if(mp.count(Id(px+dx,py+dy))){
					pii pi=mp[Id(px+dx,py+dy)];
					ll ql=pi.first,qr=pi.second;
					vector<ll>vc;
					rep(i,l,r)vc.emplace_back(id[i]);
					rep(i,ql,qr)vc.emplace_back(id[i]);
					inplace_merge(vc.begin(),vc.begin()+r-l+1,vc.end());
					rep(i,0,(ll)vc.size()-1){
						rep(j,i+1,min((ll)vc.size()-1,i+3))to[vc[j]].push_back(vc[i]);
					}
				}
			}
			l=r+1;
		}
	}
	rep(i,1,n)blk[i]=val[i]=INF;
	rep(i,1,n){
		for(ll j:to[i])Upd(j,Dis(j,i));
		for(pii qd:vq[i])ans[qd.second]=min(ans[qd.second],Query(qd.first));
	}
	rep(i,1,q)write(ans[i]),putchar('\n');
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 29052kb

input:

5 5
2 4
1 1
3 3
5 1
4 2
1 5
2 3
2 4
3 5
1 3

output:

2
8
8
2
2

result:

ok 5 number(s): "2 8 8 2 2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 28412kb

input:

2 1
1 1
1 1
1 2

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 29160kb

input:

2 1
1 100000000
100000000 1
1 2

output:

19999999600000002

result:

ok 1 number(s): "19999999600000002"

Test #4:

score: -100
Wrong Answer
time: 101ms
memory: 70692kb

input:

20000 250000
3 10
5 4
4 7
1 5
2 1
10 6
2 3
8 4
2 1
8 5
9 8
7 7
4 5
2 7
9 4
9 10
3 2
9 5
10 2
9 2
3 1
9 9
6 5
9 5
9 10
9 1
1 2
8 8
3 4
7 6
6 2
6 8
6 6
8 4
10 2
1 1
10 2
8 3
4 4
5 5
5 1
4 9
7 6
6 8
6 4
1 6
10 3
3 2
4 10
6 8
9 7
2 10
7 8
10 7
3 2
5 1
6 4
7 9
1 3
4 9
4 8
9 4
5 2
2 2
9 2
9 2
9 6
6 9
8 7
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 935th numbers differ - expected: '0', found: '1'