[Solution] Field Photography Codeforces Solution
Pak Chanek is traveling to Manado. It turns out that OSN (Indonesian National Scientific Olympiad) 2019 is being held. The contestants of OSN 2019 are currently lining up in a field to be photographed. The field is shaped like a grid of size with rows and columns. The rows are numbered from to from north to south, the columns are numbered from to from west to east. The tile in row and column is denoted as .
There are provinces that participate in OSN 2019. Initially, each contestant who represents province stands in tile for each satisfying . So, we can see that there are contestants who represent province .
Pak Chanek has a variable that is initially equal to . In one operation, Pak Chanek can choose a row and a positive integer . Then, Pak Chanek will do one of the two following possibilities:
- Move all contestants in row exactly tiles to the west. In other words, a contestant who is in is moved to .
- Move all contestants in row exactly tiles to the east. In other words, a contestant who is in is moved to .
After each operation, the value of will change into , with being the bitwise OR operation. Note that Pak Chanek can do operations to the same row more than once. Also note that Pak Chanek is not allowed to move contestants out of the grid.
There are questions. For the -th question, you are given a positive integer , Pak Chanek must do zero or more operations so that the final value of is exactly . Define as the biggest number such that after all operations, there is at least one column that contains exactly contestants. For each question, you must find the biggest possible for all sequences of operations that can be done by Pak Chanek. Note that the operations done by Pak Chanek for one question do not carry over to other questions.
The first line contains a single integer () — the number of rows in the grid and also the number of provinces that participate in OSN 2019.
The -th of the next lines contains two integers and () describing the positions of the contestants who represent province .
The next line contains a single integer () — the number of questions.
The -th of the next lines contains a single integer () — the required final value of for the -th question.
Output lines, with the -th line containing an integer that is the answer to the -th question.
No comments:
Post a Comment