forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subrectangle-queries.cpp
58 lines (48 loc) · 1.47 KB
/
subrectangle-queries.cpp
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
// Time: ctor: O(1)
// update: O(1)
// get: O(u), u is the number of updates
// Space: O(u)
class SubrectangleQueries {
public:
SubrectangleQueries(vector<vector<int>>& rectangle)
: rectangle_(rectangle) {
}
void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
updates_.emplace_back(row1, col1, row2, col2, newValue);
}
int getValue(int row, int col) {
for (int i = updates_.size() - 1; i >= 0; --i) {
const auto& [row1, col1, row2, col2, newValue] = updates_[i];
if (row1 <= row && row <= row2 &&
col1 <= col && col <= col2) {
return newValue;
}
}
return rectangle_[row][col];
}
private:
const vector<vector<int>>& rectangle_;
vector<tuple<int, int, int, int, int>> updates_;
};
// Time: ctor: O(1)
// update: O(m * n)
// get: O(1)
// Space: O(1)
class SubrectangleQueries2 {
public:
SubrectangleQueries2(vector<vector<int>>& rectangle)
: rectangle_(rectangle) {
}
void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for (int r = row1; r <= row2; ++r) {
for (int c = col1; c <= col2; ++c) {
rectangle_[r][c] = newValue;
}
}
}
int getValue(int row, int col) {
return rectangle_[row][col];
}
private:
vector<vector<int>>& rectangle_;
};