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 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.

解题思路:题目要求起点到终点的经过的每一个点的权值都为偶数,偶数的组合:奇+奇=偶、偶+偶=偶,利用数组row[]、col[],假设row[i]=x,表示从x开始到i的奇偶性相同,col[]同理,那实际上两个数组的值会组成一块区域,看终点是否存在这一个区域里面,需要保证的是起点的x、y都大于终点的x、y,如果起点的x、y都小于终点的x、y,那么起点可以说是终点,终点可以说是起点,如果起点只有x或y小于终点的x或y,相当于两个对称的点。

AC代码:

#include <iostream>
#include <cstdio>
#define N 100005

using namespace std;

int R[N],C[N];
int col[N],row[N];
int main() {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        scanf("%d", &R[i]);
    for (int i = 1; i <= n; i++)
        scanf("%d", &C[i]);
    row[1] = col[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (R[i]%2 == R[i-1]%2)
            row[i] = row[i - 1];
        else
            row[i] = i;
        if (C[i]%2 == C[i-1]%2)
            col[i] = col[i - 1];
        else
            col[i] = i;
    }
    while (q--) {
        int ra, ca, rb, cb;
        scanf("%d%d%d%d", &ra, &ca, &rb, &cb);
        if(ra<rb)
            swap(ra,rb);
        if(ca<cb)
            swap(ca,cb);
            if (row[ra] <= rb && col[ca] <= cb)
                puts("YES");
            else
                puts("NO");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lovelcy/p/11824591.html

时间: 2024-10-09 00:09:22

2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path的相关文章

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 Jakarta is planning to build two new buildings. These buildings should have a shape of a rectangle of the same size. Now, their problem is to find lan

2018 ICPC Asia Jakarta Regional Contest

题目传送门 题号 A B C D E F G H I J K L 状态 Ο . . Ο . . . . Ο . . Ο Ο:当场 Ø:已补 .  :  待补 A. Edit Distance Thinking:kk pai爷 Code:kk 不能直接反转,比如"010101",直接反转后就变成"101010",右移一位,然后加个0就可以了. 所以要先统计01的数量,如果0大于1,就全变成1,1大于0,就全变成0(从数量上的改变就大于s/2了),相等的话,就看首位是0

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 (Gym - 102082)

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 A - Digits Are Not Just Characters 签到. B - Arithmetic Progressions 题意:从给定的集合中选出最多的数构成等差数列. 题解:数字排序后,设\(dp[i][j]\)表示等差数列最后一个数字为\(a[i]\),倒数第二个数字为\(a[j]\)的最大个数.然后对于每一位枚举 \(i\),\(lower\_bound()\)找有无合法的

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Krak&#243;w

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik's RectangleProblem B: What does the fox say?Problem C: Magical GCDProblem D: SubwayProblem E: EscapeProblem F: DraughtsProblem G: History courseProblem H: C

The 43rd ACM International Collegiate Programming Contest Asia Shenyang Regional Contest

The 43rd ACM International Collegiate Programming Contest Asia Shenyang Regional Contest 原文地址:https://www.cnblogs.com/Accpted/p/11298233.html

2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)

题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine the area of the largest triangle that can be formed using \(3\) of those \(N\) points. If there is no triangle that can be formed, the answer is \(0\

比赛:ICPC Asia Taipei-Hsinchu Regional 2019 2020.4.1

C. Are They All Integers? 题意:是给定n个数字判断对任意的(a[j]-a[i])/a[k]一定要是整数. 题解: 一开始以为用枚举会超时不过还是试了下,没想到没超时,直接过了,题目很简单,简单枚举就好. #include<iostream> using namespace std; int main(){ int n,a[1001],sum; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } for(i

2019 ICPC Asia Yinchuan Regional

目录 Contest Info Solutions A. Girls Band Party B. So Easy D. Easy Problem E. XOR Tree F. Function! G. Pot!! H. Delivery Route I. Base62 K. Largest Common Submatrix N. Fibonacci Sequence Contest Info Practice Link Solved A B C D E F G H I J K L M N 9/1

The 2019 ICPC Asia Shanghai Regional Contest-B-Prefix Code

知识点:字典树. 题目链接: https://ac.nowcoder.com/acm/contest/4370/B 题意:t组数据,n个数字,问是否满足不存在任何一个数字是其他数字的前缀. 题解:套用字典树一个一个插入字符串.若在插入过程中遇到如下两种情况,则存在其中一个是另一个的前缀. 1.遍历完整个字符串都没有创造新的节点,说明该字符串是之前某个串的字串. 2.遍历过程中遇到了某个字符串的终点,说明存在一个字符串是该字符串的前缀. 若都不存在,则满足条件. AC代码: 1 #include