Two Dimensional OR

5

1 votes
Problem

Given an n by m two dimensional 2D grid containing positive integers values, you have to answer Q queries. 

In the ith query, you will be given the row and column of a start cell, (x1,y1), and the row and column of an end cell, (x2,y2). It is guaranteed that x1<=x2, and y1<=y2. For each query, you should return the Bitwise OR  of the values in all cells (i,j) satisfying the condition x1<=i<=x2, and y1<=j<=y2.

 

INPUT FORMAT

The first line contains two integers n (1<=n<=103), and m (1<=m<=103) - denoting the number of rows and columns in the grid. 

The ith line among the next n lines each contains m integers, ai,1,ai,2,ai,3,...,ai,m (1<=ai,j<230).

The next line contains an integer Q (1<=Q<=105) - denoting the number of queries. 

The remaining Q lines each contain 4 integers x1,y1,x2,y2 (1<=x1,x2<=n, 1<=y1,y2<=mx1<=x2, y1<=y2)  - denoting the coordinates of the start and end cell, respectively. 

 

OUTPUT FORMAT

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

 

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

?