PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000237. Tổng các tổng hình chữ nhật con (Sum of All Subrectangle Sums)
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 các tổng hình chữ nhật con (Sum of All Subrectangle Sums)
Sum of All Subrectangle Sums
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given an integer matrix of size , sum the elements of every nonempty axis-aligned subrectangle and add all those sums. Different boundaries define distinct rectangles. Do not take a modulus. The exact answer may be negative or exceed signed 64-bit range.
Input
The first line contains , followed by rows of integers.
Output
Print the exact integer in decimal notation.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; ; or .
- Subtask 3 (50 points): ; .
Example 1
Input
2 2
1 2
3 4
Output
40
Explanation
Each cell belongs to four subrectangles; the answer is 4×(1+2+3+4)=40.
Example 2
Input
1 2
-4 1
Output
-6
Explanation
The three segments sum to −4,1,−3; their total is −6.