RoAlgo PreOJI 2024 VIII | matmare

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 256MB Input: matmare.in Output: matmare.out

Task

You are given two sequences, aa of size nn and bb of size mm, both indexed from 11. A matrix matmat of nn rows and mm columns is constructed where mati,j=aibjmat_{i, j} = a_i | b_j. The operation xyx | y for natural numbers xx and yy refers to the bitwise OR operation. The operation is performed as follows: the numbers xx and yy are written in base 22, and the number xyx | y has bit ii active if and only if either xx has the respective bit active or yy has the respective bit active.

You are given qq queries of the form: given a submatrix, calculate the sum of the values in it.

Note: In C++, the bitwise OR operation can be implemented as "xyx | y".

Input data

The input is read from the file matmare.in. The first line contains nn, mm, qq. The second line contains the sequence aa of nn elements. The third line contains the sequence bb of mm elements.

The next qq lines each contain four values ll, cc, xx, yy, representing a query about the submatrix between rows ll and xx and columns cc and yy.

Output data

The output will be written to the file matmare.out. It will contain qq lines, each containing a single number: the sum of the submatrix for the ii-th query.

Constraints and clarifications

  • 0ai,bi<2260 \leq a_i, b_i < 2^{26}
  • n,m,q200000n, m, q \leq 200\,000
# Points Constraints
1 20 n,m,q200n, m, q \leq 200
2 20 n,m2000n, m \leq 2\,000
3 40 ai,bi1a_i, b_i \leq 1
4 20 No additional constraints

Example

matmare.in

3 4 2
1 4 3
4 6 1 0
1 1 3 4
1 2 3 3

matmare.out

53
29

Explanation

The resulting matrix is as follows:

5 7 1 1
4 6 5 4
7 7 3 3

For the first query, the sum of the elements in the entire matrix is 5+7+1+1+4+6+5+4+7+7+3+3=535+7+1+1+4+6+5+4+7+7+3+3 = 53.

For the second query, the sum of the elements in the submatrix (1,2),(3,3)(1,2),(3,3) is 7+1+6+5+7+3=297+1+6+5+7+3 = 29.

Log in or sign up to be able to send submissions!