Xor Rectangle
Practice
3.9 (17 votes)
Approved
Basic programming
Bit manipulation
Math
Medium
Problem
83% Success 4780 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You have an array of N integers \(S_1, S_2, ... S_N\). Consider a N x N matrix such that \(A_{i,j} = S_i \oplus S_j\). Here \(\oplus\) indicates bitwise xor operator.
You have been given Q queries. Each query consists of four integers \(x_1, y_1, x_2, y_2\) which denotes a submatrix with (\(x_1, y_1\)) as their top-left corner and (\(x_2, y_2\)) as their bottom-right corner such that \(x_1 \le x_2\) and \(y_1 \le y_2\). For each of the query, you have to print summation of all integers lying in queried submatrix.

INPUT:
First line of input consists of integer N. Next line consist of N space separated integers denoting array \(S_1, S_2, ... S_N\). Next line consists of integer Q denoting total number of queries. Next Q lines consist of four integers \(x_1, y_1, x_2, y_2\) denoting the queried submatrix.

OUTPUT:
For each of the query, print a single number in a separate line which denotes summation of all the numbers lying in queried submatrix.

CONSTRAINTS:
\(1 \le N \le 10^6\)
\(1 \le S_i \le 10^6\)
\(1 \le Q \le 10^6\)
\(1 \le x_1 \le x_2 \le N\)
\(1 \le y_1 \le y_2 \le N\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
15 votes
Tags:
AlgorithmsApprovedBit manipulationMediumOpen
Points:30
6 votes
Tags:
Bit ManipulationBasics of Bit ManipulationBasic ProgrammingObservationMathImplementation
Points:30
4 votes
Tags:
Basic ProgrammingBit ManipulationBit manipulationMedium