RainbowDash 的 Robot

H RainbowDash 的 Robot - 第七届校赛正式赛 ------ 补题

题目大意:

给一个 n ∗ m n*m n∗m 的二维网格,在第 i i i 列中,前 a i a_i ai 单元格被阻断,无法通行,即 [ 1 , a i ] [1,a_i] [1,ai] 。

一个机器人正在这个网格内移动,你可以向他发送指令,使得其向 上下左右 移动,但是这个机器人是由缺陷的,每条指令会执行 k k k 次,因此,机器人每次移动 k k k 个单元格,如果机器人试图移动到受阻的单元格或网格外,就会爆炸。

有 q q q 次询问,每次询问都有一个起始单元格 s x , s y sx,sy sx,sy ,一个终点单元格 f x , f y fx,fy fx,fy 和一个值 k k k 。

若机器人执行任意次命令,能否从起点单元到达终点单元

1 < = n < = 1 0 9 , 1 < = m < = 2 ∗ 1 0 5 1<=n<=10^9,1<=m<=2*10^5 1<=n<=109,1<=m<=2∗105

0 < = a i < = n 0<=a_i<=n 0<=ai<=n

1 < = q < = 2 ∗ 1 0 5 1<=q<=2*10^5 1<=q<=2∗105

1 < = k < = 1 0 9 1<=k<=10^9 1<=k<=109

保证起点坐标和终点坐标有效

思路:

优先考虑机器人从起点坐标走到终点坐标需要满足的条件,

s x sx sx 到 f x fx fx 之间距离必须是 k k k 的倍数, s y sy sy 到 f y fy fy 之间距离必须是 k k k 的倍数

cpp 复制代码
if( llabs(sx-fx)%k!=0 || llabs(sy-fy) ){
	cout<<"No\n";
	continue;
}

满足以上条件之后,考虑从 s y sy sy 到达 f y fy fy ,需要满足之间没有受阻的网格,所以贪心的想,优先走到该列最下面,再往左右走,考虑从第 s y sy sy 到 f y fy fy 之间最大的受阻网格为多少即可,数据范围较大,维护区间最值可选用线段树 。

cpp 复制代码
int len=sx+(n-sx)/k*k;
auto x=query(1,1,m,min(sy,fy),max(sy,fy));
if(x.mx>=len){
	cout<<"No\n";
}else{
	cout<<"Yes\n";
}

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define PII pair<int,int>
#define lowbit(x) x&-x
#define ALL(x) x.begin(),x.end()

const int mod = 1e9 + 7;
const int N = 1e6 + 10;

int a[N];
struct node
{
	int lz;
	int mx;
}tr[N*4];

node push_up(node L,node R)
{
	node res={0,0};
	res.mx=max(L.mx,R.mx);
	return res; 
}

void build(int p,int l,int r)
{
	if(l==r){
		tr[p].mx=a[l];
		return ;
	}
	int mid=l+r>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	tr[p]=push_up(tr[p*2],tr[p*2+1]);
}

node query(int p,int l,int r,int L,int R)
{
	if(L<=l&&r<=R){
		return tr[p];
	}
	node res={0,0};
	int mid=l+r>>1;
	if(L<=mid) res=push_up(res,query(p*2,l,mid,L,R));
	if(R>mid) res=push_up(res,query(p*2+1,mid+1,r,L,R));
	return res;
}

void solve() {
	int n,m;cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a[i];
	}	
	build(1,1,m);
	int q;cin>>q;
	while(q--){
		int sx,sy,fx,fy,k;cin>>sx>>sy>>fx>>fy>>k;
		if(llabs(sx-fx)%k!=0||llabs(sy-fy)%k!=0){
			cout<<"No\n";
			continue;
		}
		auto x=query(1,1,m,min(sy,fy),max(sy,fy));
		int len=sx+(n-sx)/k*k;
//		cout<<x.mx<<" "<<len<<'\n';
		if(x.mx>=len){
			cout<<"No\n";
		}else{
			cout<<"Yes\n";
		}
	}
}

signed main() {
	std::ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T = 1;
//	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}
相关推荐
.格子衫.26 分钟前
022数据结构之树状数组——算法备赛
数据结构·算法·1024程序员节
黑科技Python38 分钟前
生活中的“小智慧”——认识算法
学习·算法·生活
sali-tec2 小时前
C# 基于halcon的视觉工作流-章52-生成标定板
开发语言·图像处理·人工智能·算法·计算机视觉
IT古董2 小时前
【第五章:计算机视觉-项目实战之推荐/广告系统】2.粗排算法-(4)粗排算法模型多目标算法(Multi Task Learning)及目标融合
人工智能·算法·1024程序员节
熬了夜的程序员2 小时前
【LeetCode】89. 格雷编码
算法·leetcode·链表·职场和发展·矩阵
對玛祷至昏2 小时前
数据结构理论知识
数据结构·算法·排序算法
oliveira-time2 小时前
二分搜索(Binary Search)
算法
王哈哈^_^3 小时前
【数据集】【YOLO】【目标检测】口罩数据集,口罩佩戴识别数据集 1971 张,YOLO佩戴口罩检测算法实战训练教程。
人工智能·算法·yolo·目标检测·计算机视觉·ai·视觉检测
dragoooon344 小时前
[优选算法专题四.前缀和——NO.31~32 连续数组、矩阵区域和]
数据结构·算法·leetcode·1024程序员节
py有趣4 小时前
LeetCode算法学习之移除元素
java·数据结构·算法