Nandu and Monsters

2

1 votes
Problem

Nandu is stuck in a maze consisting of N rooms. Each room with room number x has a door leading into room number 2x (if 2x<=N) and another door leading into room number 2x+1 (if 2x+1<=N). All these doors are 2-way doors ie. they can be opened from both the sides.

Some of these N rooms have monsters living in them.

Nandu is currently in room number i . Nandu's only escape from this maze is to somehow reach room number j which contains the magical Wish-Granting Well. The Well will only grant a wish to Nandu if he offers it a single golden coin. Nandu presently has only a single golden coin in his possession. However, if while going from room number i to j he meets a monster, the monster will take away the only golden coin Nandu has and Nandu will never be able to escape from the maze.

You will be given information about every room telling you whether a monster resides in it or not. You will then be given Q scenarios, each scenario describing the locations i (the room currently Nandu is present in) and j (the room where the wishing well is present), i and j will vary over different scenarios but the locations of the monsters will remain fixed for all these scenarios. For each of these scenarios, you have to find out whether Nandu can escape from the maze or not.

Input :

The first line consists on N and Q denoting the number of rooms in the maze and the number of different scenarios that will be given to you. The next line consists of N non-space separated integers such that if the kth integer is '0' , there is no monster present in room number k and if the kth integer is '1' , there is a monster present in room number k. The next Q lines are such that each line consists of two space separated integers i and j denoting Nandu's current location and the location of the Wishing Well for this particular scenario respectively.

Output :

For every scenario print on a new line 'Yes' if Nandu can escape from the maze and 'No' if he cannot escape from the maze. (Quotes for clarity).

Constraints :

1<=N<=100000

1<=Q<=100000

1<=i,j<=N

Author : Shreyans

Tester : Sayan

(By IIT Kgp HackerEarth Programming Club)

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

A monster is present only in room number 3.

Scenario 1 : Nandu is initially present in room number 1 and the Wish-Granting Well is present in room number 2 . Since there is no monster in either of the rooms Nandu goes from 1 -> 2 without losing his golden coin. Hence Nandu can escape the maze by making a wish to the Wish-Granting Well

Scenario 2 : Nandu goes from 1 -> 3. However, there is a monster present in room number 3, which takes away his only golden coin before he can make a wish to the Wish-Granting Well. Hence Nandu cannot escape the maze.

Editor Image

?