Alice and Bob are playing a board game. They have n×n boards and two arrays a and b of length n. The value of each cell in the ith row and jth row is a[i]+b[j]. Alice asks q questions to Bob. In each question, Alice provides two cells A and B. She asks the following questions to Bob:
Are there any paths from A to B that contains the same parity as A and B.

Input format
  • First line: An integer n denoting the length of arrays
  • Second line: n integers with ai representing array a
  • Third line: n integers with bi representing array b
  • Fourth line: An integer q  denoting the number of test cases
  • For each test case:
    • First line: Two integers r1,c1 denoting the row and the column of A
    • Second line: Two integers r2,c2denoting the row and the column of B
Output format
For each query, if there exists a path (for example, C) from A to B that contains the same parity as A and B, then print YES. If the parity of A and B are different, then print NO.

Input


3
0 3 1
1 5 3
2
2 1
3 3
3 1
1 3
Output


YES
NO

Solution

import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n+1];
int b[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=s.nextInt();
for(int i=1;i<=n;i++)
b[i]=s.nextInt();
int t=s.nextInt();
for(int j=0;j<t;j++)
{
int x=s.nextInt(); 
int y=s.nextInt();
int xx=s.nextInt(); 
int yy=s.nextInt(); 
int an=a[x]+b[y];
int aa=a[xx]+b[yy];
if((an|1)==(aa|1))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}

}



if you have any problem in this code, take a screen shot and contact with Rishab Chaubey.

Post a Comment

Previous Post Next Post