PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000209. Tổng hình chữ nhật trên ma trận tĩnh (Range Sum Query 2D - Immutable)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng hình chữ nhật trên ma trận tĩnh (Range Sum Query 2D - Immutable)
Range Sum Query 2D - Immutable
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a static integer matrix with rows and columns, indexed from 0, return the sum of all entries in each inclusive rectangle from to . Values may be negative. Aim for query time after preprocessing.
Input
The first line contains . The next lines each contain integers . The following lines contain .
Output
Print lines, one rectangle sum per query.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; ; .
- Subtask 3 (50 points): ; ; ; ; .
Example 1
Input
5 5 3
3 0 1 4 2
5 6 3 2 1
1 2 0 1 5
4 1 0 1 7
1 0 3 0 5
2 1 4 3
1 1 2 2
1 2 2 4
Output
8
11
12
Explanation
The sums are (2+0+1)+(1+0+1)+(0+3+0)=8, (6+3)+(2+0)=11, and (3+2+1)+(0+1+5)=12.
Example 2
Input
2 3 2
-2 0 3
5 -4 1
0 0 1 2
0 0 0 0
Output
3
-2
Explanation
The full matrix sums to 3; cell (0,0) is -2.