Ingenious Metro

Time limit1sMemory limit128 MB

Summary
Given teleporter positions, each move reflects the current station across a chosen teleporter; decide for each query whether start can reach destination.
Level

Medium7 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

The Kingdom of Logonia is about to open a revolutionary new metro line, built on an invention of the Royal Engineers that enables teleportation.

The metro is a very long tunnel with a station every kilometer, so each station sits at an integer position on a number line. Among the stations, TT of them contain a teleporter. Every station has a keyboard with TT keys, one per teleporter.

The metro works like this: a passenger stands at some station (the start station) and presses the key of the teleporter they want to use. They are then moved to the station that lies at the same distance from that teleporter as the start station, but on the opposite side of it. Formally, if the start station is at position ii and the passenger presses the key of the teleporter located at position jj, they arrive at position 2×j−i2 \times j - i.

For example, with teleporters AA, BB, and CC, a passenger at station 66 who wants to reach station −2-2 could first use teleporter CC (moving from 66 to 1010) and then teleporter AA (moving from 1010 to −2-2).

It may happen that no sequence of teleporters can take a passenger from a given station XX to a given station YY. To stop passengers from trying to reach places they cannot, the King wants a program that, given the position of every teleporter, answers a series of queries. Each query gives a start station and a destination station, and the program must decide whether the passenger can travel from the start to the destination.

Input

The input consists of several test cases.

Each test case begins with a line containing two integers TT and QQ: the number of teleporters (1≤T≤1051 \le T \le 10^5) and the number of queries (1≤Q≤101 \le Q \le 10). The next line contains TT distinct integers tit_i, the positions of the teleporters (−107≤ti≤107-10^7 \le t_i \le 10^7). Each of the following QQ lines describes one query with two distinct integers SS and DD, the positions of the start and destination stations (−107≤S,D≤107-10^7 \le S, D \le 10^7).

The input ends with a line containing two zeros, which must not be processed.

Output

For each test case, print a single line with the answers to its QQ queries, in the order the queries are given. For each query print the uppercase letter YY if the passenger can reach the destination from the start using the metro, or the uppercase letter NN otherwise. Separate the answers on a line with single spaces.

Examples1

  1. Example 1

    Input
    1 1
    -2
    -6 2
    5 2
    10 20 30 40 50
    10 15
    20 40
    5 3
    0 5 -3 -8 4
    -1 499
    4 237
    -1 -591
    0 0
    
    Expected output
    Y
    N Y
    Y N Y