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();}
}

二分+思维点点之间最大距离相关推荐

  1. 【LeetCode 每日一题】1617. 统计子树中城市之间最大距离(hard)

    1617. 统计子树中城市之间最大距离   图论啊,那我先寄为敬.今天是代码搬运工.   题意很好理解啊,就是给你一个图,让你返回所有直径对应的子树的数量.是的没错,是所有直径,你不仅要枚举 1 − ...

  2. python二维、三维、思维数组之间的转化

    python二维.三维.思维数组之间的转化 import numpy as np# 假设你有一个二维数组,形状为(32,10000) gray = np.random.randint(0, 256, ...

  3. LeetCode 1617. 统计子树中城市之间最大距离(枚举所有可能+图的最大直径)

    文章目录 1. 题目 2. 解题 1. 题目 给你 n 个城市,编号为从 1 到 n .同时给你一个大小为 n-1 的数组 edges ,其中 edges[i] = [ui, vi] 表示城市 ui ...

  4. Showstopper [POJ3484] [二分] [思维]

    Description 给你n个数列,问哪一个数字在所有的数列中出现了奇数次(最多一个). Sample Input 1 10 1 2 10 11 10 1 1 10 11 10 1 4 4 1 1 ...

  5. 【题解】CF1070E Getting Deals Done(二分+思维)难度⭐⭐⭐

    CF1070E Getting Deals Done 题意翻译 题目描述 Polycarp有很多工作要做.最近他学会了一条新的时间管理技巧:"如果任务需要五分钟或更短时间,请立即执行&quo ...

  6. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  7. 2019 Multi-University Training Contest 1 - 1004 - Vacation - 二分 - 思维

    http://acm.hdu.edu.cn/showproblem.php?pid=6581 一开始想了好几个假算法.但是启发了一下潘哥,假如时间知道的话就可以从头开始确定各个车的位置.那么直接 \( ...

  8. CodeForces - 1370D Odd-Even Subsequence(二分+思维)

    题目链接:点击查看 题目大意:给出一个数组 a ,现在要求选出一个长度恰好为 k 的子序列 s ,使得的值最小 题目分析:当知道了 k 的大小之后,就可以计算出子序列奇数位置和偶数位置各有多少个数了, ...

  9. CH - 0805 防线(二分+思维)

    题目链接:点击查看 题目大意:假设现在有一个数轴,一共有n组防具,每组防具给出的形式为S,D,E,在数轴上的分布为:S,S+D,S+2D....S+KD,保证S+KD<=E 现在规定数轴上某个点 ...

最新文章

  1. 营销自动化为什么能吸引企业的喜欢?它有何魅力?
  2. 黑客组织利用El Machete窃取全球政府超过100G数据
  3. 集美大学1511,1512软件工程课程作业总结
  4. 驰骋工作流引擎设计系列10时效考核规则设计
  5. 计划策略70 之 MIXED MRP(mixed MRP)
  6. jsf netbeans_NetBeans Java EE技巧9:从数据库创建JSF应用程序
  7. linux笔记_20150825_linux下的软件工具唠叨下
  8. mysql 可视化监控_基于Prometheus构建MySQL可视化监控平台
  9. Linux平台安装go并配置go环境
  10. BeanDefinition源码解析
  11. idea 新建ssm java ee_IntelliJ IDEA基于SpringBoot如何搭建SSM开发环境的步骤详解
  12. 动易模板——制作前的准备工作
  13. CC2500的CCA
  14. SECS/GEM实现(一)半导体通讯协议软件,C、C++使用介绍
  15. 如何用饼状图显示数据
  16. 广告竞价中为什么采用二价计费
  17. 华为路由交换学习篇-链路聚合
  18. k30pro杀进程严重怎么解决_iPhonex发烫严重不开机怎么办?苹果X开机不充电关机充电解决办法...
  19. Transfomer XL翻译
  20. windows记事本特别注意

热门文章

  1. 中国管道防爆阻火器市场投资分析与需求前景预测报告2022-2028年
  2. WebBrowser 怎么样可以不保存SESSION?急!!!!!!!!!!!!
  3. 飞蛾投火式的瞬间灿烂
  4. 2.1安装前的准备工作---安装Red Hat Linux
  5. 我的世界java萌新须知_我的世界新手入门攻略 萌新前期注意事项
  6. 高效能屌丝创业者的七项习惯
  7. 服务器系统lede,[OpenWrt Wiki] 系统配置
  8. 关于自学编程,这些秒办法助你一臂之力(全文干货,建议收藏)
  9. 想进BAT?这些测试面试题助你一臂之力(附答案)
  10. 广丰计算机技术学院,广丰区五都镇中学祝晓旺——信息技术教育的拓荒者