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×N. The value of each cell is generated from a given array RR and CC of NN integers each. Specifically, the value on the ithith row and jthjth column, cell (i,j)(i,j), is equal to Ri+CjRi+Cj. 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)(r1,c1),(r2,c2),…,(rk,ck) such that |ri−ri+1|+|ci−ci+1|=1|ri−ri+1|+|ci−ci+1|=1 for all 1≤i<k1≤i<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〉〈ra,ca,rb,cb〉 as a query, your task is to determine whether there exists an even path from cell (ra,ca)(ra,ca) to cell (rb,cb)(rb,cb). To simplify the problem, it is guaranteed that both cell (ra,ca)(ra,ca) and cell (rb,cb)(rb,cb) contain even numbers.

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

Let us consider several queries:

〈2,2,1,3〉〈2,2,1,3〉: 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〉〈4,2,4,3〉: 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〉〈5,1,3,4〉: 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).

Input

Input begins with a line containing two integers: NN QQ (2≤N≤1000002≤N≤100000; 1≤Q≤1000001≤Q≤100000) representing the size of the maze and the number of queries, respectively. The next line contains NN integers: RiRi (0≤Ri≤1060≤Ri≤106) representing the array RR. The next line contains NN integers: CiCi (0≤Ci≤1060≤Ci≤106) representing the array CC. The next QQ lines each contains four integers: rara caca rbrb cbcb (1≤ra,ca,rb,cb≤N1≤ra,ca,rb,cb≤N) representing a query of 〈ra,ca,rb,cb〉〈ra,ca,rb,cb〉. It is guaranteed that (ra,ca)(ra,ca) and (rb,cb)(rb,cb) are two different cells in the maze and both of them contain even numbers.

Output

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)(ra,ca) to cell (rb,cb)(rb,cb).

Examples

input

Copy

5 3

6 2 7 8 3

3 4 8 5 1

2 2 1 3

4 2 4 3

5 1 3 4

output

Copy

YES

YES

NO

input

Copy

3 2

30 40 49

15 20 25

2 2 3 3

1 2 2 2

output

Copy

NO

YES

Note

Explanation for the sample input/output #1

This is the example from the problem description.

题意:

给出一个行的权值Ai 列的权值Bj, 每个位的权值之和为Ai + Bj

多次询问, 每次给出x1,y1,x2,y2

问有没有那么一条路径从(x1, y1)到(x2, y2)且路径上的权值都为偶数

思路:

前缀和问题,他只能横向或纵向,也就是说,它处于奇数行的时候只能到奇数行,偶数行只能移动到偶数行,列也是。

对于偶数如果向右或者向下移动,下一行或者下一列的属性一定是偶数那么每次移动都要求是行列偶数, 一行都是偶数, 一列也都是偶数, 那么区域内都是偶数,其实就是起点跟终点横纵坐标构成的区域,若满足条件可以任意走, 我们只需要前缀和判一下x1 到 x2之间有没有奇数即可。同理对于奇数, 我们只需要判断x1跟x2之间有没有偶数即可。对于列也这样处理y1跟y2

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
int r[N];/*行 */,c[N]; // 列
int a[N],b[N];
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)scanf("%d", &a[i]), a[i] &= 1, r[i] = r[i - 1] + a[i];
for (int i = 1; i <= n; i++)scanf("%d", &b[i]), b[i] &= 1, c[i] = c[i - 1] + b[i];
for (int i = 1; i <= k; i++)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if ((a[x1] + b[y1]) % 2 || (a[x2] + b[y2]) % 2 || (a[x1] + b[y1]) != (a[x2] + b[y2]))
puts("NO");
else
{
if (x1 > x2)
swap(x1, x2);
if (y1 > y2)
swap(y1, y2);
if (a[x1] && r[x2] - r[x1 - 1] != x2 - x1 + 1 || !a[x1] && r[x2] - r[x1 - 1] != 0)
puts("NO");
else
if (b[y1] && c[y2] - c[y1 - 1] != y2 - y1 + 1 || !b[y2] && c[y2] - c[y1 - 1] != 0)
puts("NO");
else
puts("YES");
}
}
return 0;
}

2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path(思维)的更多相关文章

  1. 2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path

    Pathfinding is a task of finding a route between two points. It often appears in many problems. For ...

  2. 2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)

    2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) easy: ACE ...

  3. 2019-2020 ICPC, Asia Jakarta Regional Contest H. Twin Buildings

    As you might already know, space has always been a problem in ICPC Jakarta. To cope with this, ICPC ...

  4. 2019-2020 ICPC, Asia Jakarta Regional Contest

    目录 Contest Info Solutions A. Copying Homework C. Even Path E. Songwriter G. Performance Review H. Tw ...

  5. 2018 ICPC Asia Jakarta Regional Contest

    题目传送门 题号 A B C D E F G H I J K L 状态 Ο . . Ο . . Ø Ø Ø Ø . Ο Ο:当场 Ø:已补 .  :  待补 A. Edit Distance Thin ...

  6. 2019-2020 ICPC, Asia Jakarta Regional Contest A. Copying Homework

    Danang and Darto are classmates. They are given homework to create a permutation of N integers from  ...

  7. 模拟赛小结:2019-2020 ICPC, Asia Jakarta Regional Contest

    比赛链接:传送门 离金最近的一次?,lh大佬carry场. Problem A. Copying Homework 00:17(+) Solved by Dancepted 签到,读题有点慢了.而且配 ...

  8. 2019-2020 ICPC, Asia Jakarta Regional Contest A. Copying Homework (思维)

    Danang and Darto are classmates. They are given homework to create a permutation of N integers from  ...

  9. Asia Jakarta Regional Contest 2019 I - Mission Possible

    cf的地址 因为校强, "咕咕十段"队获得了EC-final的参赛资格 因为我弱, "咕咕十段"队现在银面很大 于是咕咕十段决定进行训练. 周末vp了一场, 这 ...

随机推荐

  1. JAVA自动化之Junit单元测试框架详解

    一.JUnit概述&配置 1.Junit是什么? Junit是一个Java 编程语言的开源测试框架,用于编写和运行测试.官网 地址:https://junit.org/junit4/ 2.Ma ...

  2. 【python】利用jieba中文分词进行词频统计

    以下代码对鲁迅的<祝福>进行了词频统计: import io import jieba txt = io.open("zhufu.txt", "r" ...

  3. .NET 下基于动态代理的 AOP 框架实现揭秘

    .NET 下基于动态代理的 AOP 框架实现揭秘 Intro 之前基于 Roslyn 实现了一个简单的条件解析引擎,想了解的可以看这篇文章 https://www.cnblogs.com/weihan ...

  4. 邮件退信“Remote Server returned '420 4.2.0 Recipient deferred because there is no Mdb'”

    标题是一个近期遇到的NDR 对于Exchange运维工作者,NDR通常给了我们较为清晰的排错方向,我们先看一下退信的原因, 我的一台MailBox报错“远程服务器返回‘420 4.2.0’接受延迟,因 ...

  5. java中JVM虚拟机内存模型详细说明

    java中JVM虚拟机内存模型详细说明 2012-12-12 18:36:03|  分类: JAVA |  标签:java  jvm  堆内存  虚拟机  |举报|字号 订阅     JVM的内部结构 ...

  6. Jmeter连接mysql数据库?so easy!!!

    一.确保mysql数据库能够通过Navicat等远程连接工具连接. 注意:一定是确保能使用navicat连接,而不是dos窗口! 比如笔者需要查询ecshop数据库下的ecs_admin_user表, ...

  7. 使用SVGDeveloper画svg地图详细过程

    使用步骤 1.  安装svg 2.  具体操作 1.     打开svg,点击file ,new,默认svg,点击ok 显示界面如下: 然后点击image 把鼠标放到代码下面的的桌面上,鼠标箭头会变成 ...

  8. 掷骰子 dp

    B. 掷骰子 单点时限: 2.0 sec 内存限制: 512 MB 骰子,中国传统民间娱乐用来投掷的博具,早在战国时期就已经被发明. 现在给你 n 个骰子,求 n 个骰子掷出点数之和为 a 的概率是多 ...

  9. ROM定制开发教程-Android adb命令用法与实例解析

    一.什么是ADB Android Debug Bridge(adb)是一个命令行工具,可让您与模拟器或连接的Android设备进行通信.您可以在android sdk / platform-tools ...

  10. OkHttp 优雅封装 HttpUtils 之 上传下载解密

    曾经在代码里放荡不羁,如今在博文中日夜兼行,只为今天与你分享成果.如果觉得本文有用,记得关注我,我将带给你更多. 还没看过第一篇文章的欢迎移步:OkHttp 优雅封装 HttpUtils 之气海雪山初 ...