#P5795. [THUSC 2015] 异或运算

[THUSC 2015] 异或运算

Description

Given a sequence X={x1,x2,,xn}X=\{x_1,x_2,\ldots,x_n\} of length nn and a sequence Y={y1,y2,,ym}Y=\{y_1,y_2,\ldots,y_m\} of length mm, define a matrix AA where the value in row ii and column jj is Ai,j=xi xor yjA_{i,j}=x_i\ \operatorname{xor}\ y_j.

For each query, you are given a rectangular region i[u,d],j[l,r]i\in[u,d],\, j\in[l,r]. Find the kk-th largest value among all Ai,jA_{i,j} in this region.

Input Format

The first line contains two positive integers n,mn,m, representing the lengths of the two sequences.

The second line contains nn non-negative integers xix_i.

The third line contains mm non-negative integers yjy_j.

The fourth line contains a positive integer pp, representing the number of queries.

The next pp lines each contain five positive integers u,d,l,r,ku,d,l,r,k, describing one query. The meanings are as stated above.

Output Format

Output pp lines. Each line contains one non-negative integer, the answer to the corresponding query.

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

Hint

For 100%100\% of the data:

  • 0Xi,Yj<2310\leq X_i,Y_j<2^{31}.
  • 1udn10001\leq u\leq d\leq n\leq 1000.
  • 1lrm3000001\leq l\leq r\leq m\leq 300000.
  • 1k(du+1)×(rl+1)1\leq k\leq (d-u+1)\times (r-l+1), 1p5001\leq p\leq 500.

Translated by ChatGPT 5