Two Dimensional OR

5

1 votes
Multi-dimensional, Bit manipulation, Prefix, Data Structures, Arrays
Problem

Given an  by  two dimensional grid containing positive integers values, you have to answer queries. 

In the  query, you will be given the row and column of a start cell, , and the row and column of an end cell, . It is guaranteed that , and . For each query, you should return the Bitwise OR  of the values in all cells  satisfying the condition , and .

 

INPUT FORMAT

The first line contains two integers  , and   - denoting the number of rows and columns in the grid. 

The  line among the next lines each contains integers,  .

The next line contains an integer  () - denoting the number of queries. 

The remaining  lines each contain  integers  (,  )  - denoting the coordinates of the start and end cell, respectively. 

 

OUTPUT FORMAT

The output should contain  lines, the  of them should be the answer to the  query. 

 

Sample Input
3 3
1 2 3
4 5 6
7 8 9
2
1 1 3 3
1 2 1 3
Sample Output
15
3
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The first query contains all the values in the grid. Hence, the answer is the bitwise OR of all the values in the grid, which is 15.

The second query contains cell (1, 2) and (1, 3). Hence, the answer to the query is the bitwise OR of 2 and 3, which is 3

Editor Image

?