二分+思维点点之间最大距离

Problem Description

There are nn towns in Byteland, labeled by 1,2,\dots,n1,2,…,n. The ii-th town's location is (x_i,y_i)(xi​,yi​). Little Q got a taxi VIP card, he can use the VIP card to cut down the taxi fare. Formally, assume Little Q is at (x',y')(x′,y′), if he calls a taxi to drive him to the kk-th town, the VIP card will reduce \min(|x'-x_k|+|y'-y_k|,w_k)min(∣x′−xk​∣+∣y′−yk​∣,wk​) dollars.

Little Q wants to make full use of his VIP card. He will give you qq queries, in each query you will be given his location, and you need to choose a town such that the VIP card will reduce the most taxi fare.

Input

The first line contains a single integer TT (1 \leq T \leq 1001≤T≤100), the number of test cases. For each test case:

The first line contains two integers nn and qq (1 \leq n,q \leq 100,0001≤n,q≤100,000), denoting the number of towns and the number of queries.

Each of the following nn lines contains three integers x_ixi​, y_iyi​ and w_iwi​ (1 \leq x_i,y_i,w_i \leq 10^91≤xi​,yi​,wi​≤109), describing a town.

Each of the following qq lines contains two integers x'x′ and y'y′ (1 \leq x',y' \leq 10^91≤x′,y′≤109), describing a query.

It is guaranteed that the sum of all nn is at most 500,000500,000, and the sum of all qq is at most 500,000500,000.

Output

For each query, print a single line containing an integer, denoting the maximum possible reduced taxi fare.

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+5,INF=2e9;
struct node{
	int x,y,w;
	bool operator < (const node &b)
	{
		return w<b.w;
	}
}p[N];
int a[N],b[N],c[N],d[N];
int xx,yy;
int ans=0;
bool pd(int mid)
{
	int mxv=0;
	mxv=max(mxv,xx+yy+a[mid]);
	mxv=max(mxv,xx-yy+b[mid]);
	mxv=max(mxv,-xx+yy+c[mid]);
	mxv=max(mxv,-xx-yy+d[mid]);
	ans=max(ans,min(mxv,p[mid].w));
	return mxv>=p[mid].w;
}
void solve()
{
	int n,q;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
	{
		int x,y,w;
		scanf("%d%d%d",&x,&y,&w);
		p[i]={x,y,w};
	}
	sort(p+1,p+1+n);
	a[n+1]=b[n+1]=c[n+1]=d[n+1]=-INF;
	for(int i=n;i>=1;i--)
	{
		a[i]=max(a[i+1],-p[i].x-p[i].y);
		b[i]=max(b[i+1],-p[i].x+p[i].y);
		c[i]=max(c[i+1],p[i].x-p[i].y);
		d[i]=max(d[i+1],p[i].x+p[i].y);
	}
	while(q--)
	{
		scanf("%d%d",&xx,&yy);
		ans=0;
		int l=1,r=n;
		while(l<=r)
		{
			int m=(l+r)/2;
			if(pd(m)) l=m+1;
			else r=m-1;
		}
		printf("%d\n",ans);
	}
		
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		solve();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值