#P1527. [国家集训队] 矩阵乘法

[国家集训队] 矩阵乘法

Description

Given an n×nn \times n matrix, you do not need to perform matrix multiplication. Instead, for each query, find the kk-th smallest number in a subrectangle.

Input Format

The first line contains two integers, representing the matrix size nn and the number of queries qq.

Lines 22 through (n+1)(n + 1) each contain nn integers, representing the matrix. The jj-th number on line (i+1)(i + 1) is the number in row ii and column jj of the matrix, denoted ai,ja_{i, j}.

Then qq lines follow, each containing five integers x1,y1,x2,y2,kx_1, y_1, x_2, y_2, k, representing one query. For each query, find the kk-th smallest number in the subrectangle with top-left corner (x1,y1)(x_1, y_1) and bottom-right corner (x2,y2)(x_2, y_2).

Output Format

For each query, output a single integer on its own line representing the answer.

2 2
2 1
3 4
1 2 1 2 1
1 1 2 2 3

1
3

Hint

Constraints and Conventions

  • For 20%20\% of the testdata, it is guaranteed that n100n \leq 100, q103q \leq 10^3.
  • For 40%40\% of the testdata, it is guaranteed that n300n \leq 300, q104q \leq 10^4.
  • For 60%60\% of the testdata, it is guaranteed that n400n \leq 400, q3×104q \leq 3 \times 10^4.
  • For 100%100\% of the testdata, it is guaranteed that 1n5001 \leq n \leq 500, 1q6×1041 \leq q \leq 6 \times 10^4, 0ai,j1090 \leq a_{i, j} \leq 10^9.

Translated by ChatGPT 5