1295: 实验2-1-3 一维数组模拟二维矩阵查询 / Simulating a 2D Matrix with a 1D Array

Memory Limit:128 MB Time Limit:1.000 S
Judge Style:Text Compare Creator:
Submit:126 Solved:88

Description

【中文题面】
输入一个 r 行 c 列的矩阵,但程序中必须使用一维数组保存它,不能使用二维数组保存矩阵。
矩阵按行优先顺序存入一维数组。第 x 行第 y 列元素在一维数组中的下标为 getIndex(x, y, c) = (x - 1) * c + (y - 1)。
请处理若干查询:查询一行的和、查询一列的和、查询某个位置的元素值。
[English Statement]
Given an r-by-c matrix, you must store it in a one-dimensional array. Do not store the matrix in a two-dimensional array.
The matrix is stored in row-major order. The index of row x and column y is getIndex(x, y, c) = (x - 1) * c + (y - 1).
Process queries for a row sum, a column sum, or one element value.

Input

【输入】
第一行输入 r 和 c。
接下来 r 行,每行 c 个整数,表示矩阵。
下一行输入查询数 q。
之后 q 行,每行一个查询:1 x 表示第 x 行的和;2 y 表示第 y 列的和;3 x y 表示第 x 行第 y 列的元素。
[Input]
The first line contains r and c.
The next r lines contain the matrix.
The next line contains q.
Each of the next q lines is one query: 1 x for row x sum, 2 y for column y sum, and 3 x y for one element.

Output

【输出】
对每个查询输出一行答案。
[Output]
Print one answer line for each query.

Sample Input Copy

3 4
1 2 3 4
5 6 7 8
9 10 11 12
3
1 2
2 3
3 3 4

Sample Output Copy

26
21
12

HINT

【拓展练习与 AI 使用建议】
拓展练习:把输入坐标改成从 0 开始,此时下标公式变为 x * c + y。
向 AI 提问时,明确要求必须使用 int a[10000] 这样的一维数组,并单独写 getIndex 函数,不要使用 vector 或二维数组。
读懂代码时重点检查所有访问矩阵的位置是否都调用同一个下标公式,这样改题目要求时通常只需要改很少几行。
[Extension and AI Advice]
Extension: use 0-based coordinates, so the index formula becomes x * c + y.
When prompting AI, require an int a[10000]-style one-dimensional array and a separate getIndex function. Do not use vector or a 2D array.
When reading the code, check that every matrix access uses the same index formula so a small requirement change usually affects only a few lines.