-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
64 lines (61 loc) · 1.75 KB
/
program.cpp
File metadata and controls
64 lines (61 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "../include/pre.h"
class NumMatrix
{
public:
NumMatrix(vector<vector<int>> &matrix)
{
makePartlySumArray(matrix);
}
int sumRegion(int row1, int col1, int row2, int col2)
{
row2++, col2++;
return PartlySum_[row2][col2] - PartlySum_[row1][col2] - PartlySum_[row2][col1] + PartlySum_[row1][col1];
}
private:
void makePartlySumArray(const vector<vector<int>> &matrix)
{
if (matrix.size() == 0) {
PartlySum_ = {{0}};
return;
}
auto& rst = PartlySum_;
PartlySum_.resize(matrix.size() + 1);
for (int i = 0; i != matrix.size() + 1; i++) rst[i].resize(matrix[0].size() + 1);
for (int i = 1; i != matrix.size() + 1; i++) {
for (int j = 1; j != matrix[0].size() + 1; j++) {
rst[i][j] = rst[i][j - 1] + matrix[i - 1][j - 1];
}
}
for (int i = 1; i != matrix[0].size() + 1; i++) {
for (int j = 1; j != matrix.size() + 1; j++) {
rst[j][i] = rst[j - 1][i] + rst[j][i];
}
}
for (auto& v : rst) {
for (auto i : v) {
cout << i << ", ";
}
cout << "\n";
}
}
vector<vector<int>> PartlySum_;
};
int main()
{
// vector<vector<int>> matrix = {
// {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},
// };
vector<vector<int>> matrix = {
{-4, -5},
};
NumMatrix test(matrix);
cout << test.sumRegion(0, 1, 0, 1) << "\n";
//cout << test.sumRegion(2, 1, 4, 3) << "\n";
//cout << test.sumRegion(1, 1, 2, 2) << "\n";
//cout << test.sumRegion(1, 2, 2, 4) << "\n";
return 0;
}