QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#453749#5463. Range Closest Pair of Points Queryship2077WA 138ms188728kbC++171.9kb2024-06-24 10:26:192024-06-24 10:26:20

Judging History

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

  • [2024-06-24 10:26:20]
  • 评测
  • 测评结果:WA
  • 用时:138ms
  • 内存:188728kb
  • [2024-06-24 10:26:19]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int M=2.5e5+5;
struct point{int x,y;}a[M];
int n,q,blk,idx,qr[M],bel[M];
ll ans[M],rec[M],sum1[M],sum2[M];
vector<pair<int,int>>qry[M];
unordered_map<ll,int>mp[28];
vector<int>vec[M*28];
ll dist(point a,point b){
	return 1ll*(a.x-b.x)*(a.x-b.x)+1ll*(a.y-b.y)*(a.y-b.y);
}
int read(){
	int x=0;char ch=getchar();
	while (!isdigit(ch)) ch=getchar();
	while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
	return x;
}
void update(int x,ll p){
	sum1[bel[x]]=min(sum1[bel[x]],p);sum2[x]=min(sum2[x],p);
}
ll query(int x){ ll ans=LLONG_MAX;
	for (int i=bel[x]+1;i<=bel[n];i++) ans=min(ans,sum1[i]);
	for (int i=x;i<=qr[bel[x]];i++) ans=min(ans,sum2[i]);
	return ans;
}
int main(){
	n=read();q=read();blk=ceil(sqrt(n));
	for (int i=1;i<=n;i++) rec[i]=sum1[i]=sum2[i]=LLONG_MAX;
	for (int i=1;i<=n;i++) bel[i]=(i-1)/blk+1;
	for (int i=1;i<=bel[n];i++) qr[i]=qr[i-1]+blk; qr[bel[n]]=n;
	for (int i=1;i<=n;i++) a[i]={read(),read()};
	for (int i=1;i<=q;i++){
		int l=read(),r=read();
		qry[r].emplace_back(l,i);
	}
	for (int i=1;i<=n;i++){
		for (int k=0;k<28;k++){
			for (auto x:{-1,0,1})
				for (auto y:{-1,0,1}){
					int dx=(a[i].x>>k)+x,dy=(a[i].y>>k)+y;
					if (dx<0||dy<0) continue;
					ll Hash=dx<<30|dy;
					if (!mp[k].count(Hash)) continue;
					int num=mp[k][Hash];
					vector<int>tmp;int pos=0;
					for (auto x:vec[num]){
						ll tmp=dist(a[x],a[i]);
						if (tmp<rec[x]) update(x,rec[x]=tmp);
						if (tmp<=1ll<<((k-1)<<1)) pos=x;
					}
					for (auto x:vec[num]) if (x>pos)
						tmp.emplace_back(x);
					swap(tmp,vec[num]);
				}
			ll Hash=(a[i].x>>k)<<30|(a[i].y>>k);
			if (!mp[k].count(Hash)) mp[k][Hash]=idx++;
			vec[mp[k][Hash]].emplace_back(i);
		}
		for (auto [j,id]:qry[i])
			ans[id]=query(j);
	}
	for (int i=1;i<=q;i++) printf("%lld\n",ans[i]);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 27ms
memory: 184392kb

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: 27ms
memory: 184568kb

input:

2 1
1 1
1 1
1 2

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 21ms
memory: 183824kb

input:

2 1
1 100000000
100000000 1
1 2

output:

19999999600000002

result:

ok 1 number(s): "19999999600000002"

Test #4:

score: -100
Wrong Answer
time: 138ms
memory: 188728kb

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 38262nd numbers differ - expected: '0', found: '1'