CF1252C.Even Path

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Pathfinding is a task of finding a route between two points. It often appears in many problems. For example, in a GPS navigation software where a driver can query for a suggested route, or in a robot motion planning where it should find a valid sequence of movements to do some tasks, or in a simple maze solver where it should find a valid path from one point to another point. This problem is related to solving a maze.

The maze considered in this problem is in the form of a matrix of integers AA of N×NN \times N. The value of each cell is generated from a given array RR and CC of NN integers each. Specifically, the value on the ithi^{th} row and jthj^{th} column, cell (i,j)(i,j), is equal to Ri+CjR_i + C_j. Note that all indexes in this problem are from 11 to NN.

A path in this maze is defined as a sequence of cells (r1,c1),(r2,c2),…,(rk,ck)(r_1,c_1), (r_2,c_2), \dots, (r_k,c_k) such that ∣ri−ri+1∣+∣ci−ci+1∣=1|r_i - r_{i+1}| + |c_i - c_{i+1}| = 1 for all 1≤i<k1 \le i \lt k. In other words, each adjacent cell differs only by 11 row or only by 11 column. An even path in this maze is defined as a path in which all the cells in the path contain only even numbers.

Given a tuple ⟨ra,ca,rb,cb⟩\langle r_a,c_a,r_b,c_b \rangle as a query, your task is to determine whether there exists an even path from cell (ra,ca)(r_a,c_a) to cell (rb,cb)(r_b,c_b). To simplify the problem, it is guaranteed that both cell (ra,ca)(r_a,c_a) and cell (rb,cb)(r_b,c_b) contain even numbers.

For example, let N=5N = 5, R=6,2,7,8,3R = {6, 2, 7, 8, 3}, and C=3,4,8,5,1C = {3, 4, 8, 5, 1}. The following figure depicts the matrix AA of 5×55 \times 5 which is generated from the given array RR and CC.

Let us consider several queries:

  • ⟨2,2,1,3⟩\langle 2, 2, 1, 3 \rangle: There is an even path from cell (2,2)(2,2) to cell (1,3)(1,3), e.g., (2,2),(2,3),(1,3)(2,2), (2,3), (1,3). Of course, (2,2),(1,2),(1,3)(2,2), (1,2), (1,3) is also a valid even path.
  • ⟨4,2,4,3⟩\langle 4, 2, 4, 3 \rangle: There is an even path from cell (4,2)(4,2) to cell (4,3)(4,3), namely (4,2),(4,3)(4,2), (4,3).
  • ⟨5,1,3,4⟩\langle 5, 1, 3, 4 \rangle: There is no even path from cell (5,1)(5,1) to cell (3,4)(3,4). Observe that the only two neighboring cells of (5,1)(5,1) are cell (5,2)(5,2) and cell (4,1)(4,1), and both of them contain odd numbers (7 and 11, respectively), thus, there cannot be any even path originating from cell (5,1)(5,1).

路径查找(Pathfinding)是指在两点之间寻找一条路径的任务。它经常出现在许多问题中。例如,在GPS导航软件中,驾驶员可以查询推荐路线;在机器人运动规划中,机器人需要找到一系列有效的移动动作来完成某些任务;或在一个简单的迷宫求解器中,需要找到从一个点到另一个点的有效路径。本题与求解迷宫相关。

本题所考虑的迷宫是一个 N×NN \times N 的整数矩阵 AA。每个格子的值由给定的两个长度均为 NN 的整数数组 RR 和 CC 生成。具体地,第 ii 行、第 jj 列(即格子 (i,j)(i,j))的值等于 Ri+CjR_i + C_j。注意:本题中所有下标均从 11 到 NN。

该迷宫中的一条路径定义为一组格子序列 (r1,c1),(r2,c2),…,(rk,ck)(r_1,c_1), (r_2,c_2), \dots, (r_k,c_k),满足对所有 1≤i<k1 \le i < k,均有 ∣ri−ri+1∣+∣ci−ci+1∣=1|r_i - r_{i+1}| + |c_i - c_{i+1}| = 1。换言之,任意相邻两个格子仅在行号上相差 11,或仅在列号上相差 11。该迷宫中的一条偶路径(even path) 是指路径中所有格子的值均为偶数的路径。

给定一个查询元组 ⟨ra,ca,rb,cb⟩\langle r_a,c_a,r_b,c_b \rangle,你的任务是判断是否存在一条从格子 (ra,ca)(r_a,c_a) 到格子 (rb,cb)(r_b,c_b) 的偶路径。为简化问题,保证格子 (ra,ca)(r_a,c_a) 和 (rb,cb)(r_b,c_b) 的值均为偶数。

例如,设 N=5N = 5,R={6,2,7,8,3}R = \{6, 2, 7, 8, 3\},C={3,4,8,5,1}C = \{3, 4, 8, 5, 1\}。下图展示了由给定数组 RR 和 CC 生成的 5×55 \times 5 矩阵 AA:

我们考虑如下几个查询:

  • ⟨2,2,1,3⟩\langle 2, 2, 1, 3 \rangle:存在一条从格子 (2,2)(2,2) 到格子 (1,3)(1,3) 的偶路径,例如 (2,2),(2,3),(1,3)(2,2), (2,3), (1,3)。当然,(2,2),(1,2),(1,3)(2,2), (1,2), (1,3) 也是一条合法的偶路径。
  • ⟨4,2,4,3⟩\langle 4, 2, 4, 3 \rangle:存在一条从格子 (4,2)(4,2) 到格子 (4,3)(4,3) 的偶路径,即 (4,2),(4,3)(4,2), (4,3)。
  • ⟨5,1,3,4⟩\langle 5, 1, 3, 4 \rangle:不存在从格子 (5,1)(5,1) 到格子 (3,4)(3,4) 的偶路径。观察可知,格子 (5,1)(5,1) 的两个相邻格子仅为 (5,2)(5,2) 和 (4,1)(4,1),而它们的值分别为 77 和 1111(均为奇数),因此无法从格子 (5,1)(5,1) 出发构造任何偶路径。

输入格式

Input begins with a line containing two integers: NN QQ (2≤N≤100 0002 \le N \le 100\,000; 1≤Q≤100 0001 \le Q \le 100\,000) representing the size of the maze and the number of queries, respectively. The next line contains NN integers: RiR_i (0≤Ri≤1060 \le R_i \le 10^6) representing the array RR. The next line contains NN integers: CiC_i (0≤Ci≤1060 \le C_i \le 10^6) representing the array CC. The next QQ lines each contains four integers: rar_a cac_a rbr_b cbc_b (1≤ra,ca,rb,cb≤N1 \le r_a, c_a, r_b, c_b \le N) representing a query of ⟨ra,ca,rb,cb⟩\langle r_a, c_a, r_b, c_b \rangle. It is guaranteed that (ra,ca)(r_a,c_a) and (rb,cb)(r_b,c_b) are two different cells in the maze and both of them contain even numbers.

输入的第一行包含两个整数:NN 和 QQ(2≤N≤100 0002 \le N \le 100\,000;1≤Q≤100 0001 \le Q \le 100\,000),分别表示迷宫的大小和查询次数。
第二行包含 NN 个整数:RiR_i(0≤Ri≤1060 \le R_i \le 10^6),表示数组 RR。
第三行包含 NN 个整数:CiC_i(0≤Ci≤1060 \le C_i \le 10^6),表示数组 CC。
接下来的 QQ 行,每行包含四个整数:rar_a cac_a rbr_b cbc_b(1≤ra,ca,rb,cb≤N1 \le r_a, c_a, r_b, c_b \le N),表示一次关于 ⟨ra,ca,rb,cb⟩\langle r_a, c_a, r_b, c_b \rangle 的查询。
保证 (ra,ca)(r_a,c_a) 和 (rb,cb)(r_b,c_b) 是迷宫中两个不同的格子,且这两个格子中均含有偶数。

输出格式

For each query in the same order as input, output in a line a string "YES" (without quotes) or "NO" (without quotes) whether there exists an even path from cell (ra,ca)(r_a,c_a) to cell (rb,cb)(r_b,c_b).

对于每个查询,按照输入的相同顺序,输出一行字符串“YES”(不带引号)或“NO”(不带引号),表示是否存在一条从单元格 (ra,ca)(r_a,c_a) 到单元格 (rb,cb)(r_b,c_b) 的偶路径。

输入输出样例

  • 输入#1

    5 3
    6 2 7 8 3
    3 4 8 5 1
    2 2 1 3
    4 2 4 3
    5 1 3 4

    输出#1

    YES
    YES
    NO
  • 输入#2

    3 2
    30 40 49
    15 20 25
    2 2 3 3
    1 2 2 2

    输出#2

    NO
    YES

说明/提示

Explanation for the sample input/output #1

This is the example from the problem description.

样例输入/输出 #1 的说明

这是题目描述中的示例。

输入解题思路,AI测评打分。不知道怎么写?

首页