-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0308_NumMatrix.cc
87 lines (78 loc) · 1.61 KB
/
Problem_0308_NumMatrix.cc
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
// 二维树状数组
class NumMatrix
{
public:
NumMatrix(vector<vector<int>> &matrix)
{
if (matrix.size() == 0 || matrix[0].size() == 0)
{
return;
}
N = matrix.size();
M = matrix[0].size();
tree = vector<vector<int>>(N + 1, vector<int>(M + 1));
nums = vector<vector<int>>(N, vector<int>(M));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
update(i, j, matrix[i][j]);
}
}
}
int sum(int row, int col)
{
int sum = 0;
for (int i = row + 1; i > 0; i -= lowBit(i))
{
for (int j = col + 1; j > 0; j -= lowBit(j))
{
sum += tree[i][j];
}
}
return sum;
}
void update(int row, int col, int val)
{
if (N == 0 || M == 0)
{
return;
}
int add = val - nums[row][col];
nums[row][col] = val;
for (int i = row + 1; i <= N; i += lowBit(i))
{
for (int j = col + 1; j <= M; j += lowBit(j))
{
tree[i][j] += add;
}
}
}
int sumRegion(int row1, int col1, int row2, int col2)
{
if (N == 0 || M == 0)
{
return 0;
}
return sum(row2, col2) + sum(row1 - 1, col1 - 1) - sum(row1 - 1, col2) - sum(row2, col1 - 1);
}
int lowBit(int x)
{
return x & -x;
}
private:
vector<vector<int>> tree;
vector<vector<int>> nums;
int N;
int M;
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix* obj = new NumMatrix(matrix);
* obj->update(row,col,val);
* int param_2 = obj->sumRegion(row1,col1,row2,col2);
*/