1252.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 A of N×N. The value of each cell is generated from a given array R and C of N integers each. Specifically, the value on the ith row and jth column, cell (i,j), is equal to Ri+Cj. Note that all indexes in this problem are from 1 to N.

A path in this maze is defined as a sequence of cells (r1,c1),(r2,c2),…,(rk,ck) such that |ri−ri+1|+|ci−ci+1|=1 for all 1≤i<k. In other words, each adjacent cell differs only by 1 row or only by 1 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〉 as a query, your task is to determine whether there exists an even path from cell (ra,ca) to cell (rb,cb). To simplify the problem, it is guaranteed that both cell (ra,ca) and cell (rb,cb) contain even numbers.

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

 

Let us consider several queries:

  • 〈2,2,1,3〉: There is an even path from cell (2,2) to cell (1,3), e.g., (2,2),(2,3),(1,3). Of course, (2,2),(1,2),(1,3) is also a valid even path.
  • 〈4,2,4,3〉: There is an even path from cell (4,2) to cell (4,3), namely (4,2),(4,3).
  • 〈5,1,3,4〉: There is no even path from cell (5,1) to cell (3,4). Observe that the only two neighboring cells of (5,1) are cell (5,2)and cell (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).

Input

Input begins with a line containing two integers: N Q (2≤N≤100000; 1≤Q≤100000) representing the size of the maze and the number of queries, respectively. The next line contains N integers: Ri (0≤Ri≤106) representing the array R. The next line contains N integers: Ci (0≤Ci≤106) representing the array C. The next Q lines each contains four integers: ra ca rb cb (1≤ra,ca,rb,cb≤N) representing a query of 〈ra,ca,rb,cb〉. It is guaranteed that (ra,ca) and (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) to cell (rb,cb).

Examples

input

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

YES
YES
NO

input

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

output

NO
YES

Note

Explanation for the sample input/output #1

This is the example from the problem description.

 

题意+题解:

题目中让求的是从开始点出发到终止点,要求的是经过的点都是偶数,对于给定的矩阵,输入两个数组a和b,然后a代表的是矩阵的行,b代表的是矩阵的列,然后矩阵中的项是ai+bj。我们知道奇数加奇数是偶数,偶数加偶数是偶数,其他的两种情况是奇数,所以我们从开始处进行判断,如果开始处是偶数,那么并且横纵坐标都是偶数,那么所经过的路径的横纵坐标都是偶数,如果存在奇数就不成立,反之都是奇数。

大致过程:

先判断开始点和终止点是什么,如果是奇数的话,直接输出NO,如果是偶数的话,判断这两个点的横纵坐标是不是相同类型的数(都是奇数或者都是偶数,如果一个为奇数一个为偶数就输出NO)。

接下来知道都是偶数了,不过我们只是判断了两个端点是偶数,接下来需要判断中间的过程是偶数。

判断方法是使用前缀和,如果是奇数的话,前缀和的差跟下标的差相同,如果为偶数的话,前缀和为0.

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
typedef long long ll;
using namespace std;
const int maxn=1e5+100;
int a[maxn],b[maxn];
int num1[maxn],num2[maxn];
int x1,x2,y1,y2,x;
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i)
        {

            scanf("%d",&x);
            a[i]=x%2;
            num1[i]=num1[i-1]+a[i];
        }//行
    for(int i=1;i<=n;++i)
        {
            scanf("%d",&x);
            b[i]=x%2;
            num2[i]=num2[i-1]+b[i];
        }//列
    while(m--)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if(x1>x2) swap(x1,x2);
        if(y1>y2) swap(y1,y2);
        if((a[x1]+b[y1])%2||(a[x2]+b[y2])%2||a[x1]+b[y1]!=a[x2]+b[y2])
        {
            printf("NO\n"); continue;
        }
        else
        {
            if(a[x1]&&(num1[x2]-num1[x1-1]!=x2-x1+1||num2[y2]-num2[y1-1]!=y2-y1+1))
            {
                printf("NO\n");continue;
            }
            else if(a[x1]==0&&(num1[x2]-num1[x1-1]!=0||num2[y2]-num2[y1-1]!=0))
            {
                printf("NO\n"); continue;
            }
            printf("YES\n");
        }
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值