forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
23 lines (21 loc) · 772 Bytes
/
Copy paths2.cpp
File metadata and controls
23 lines (21 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// OJ: https://leetcode.com/problems/subrectangle-queries/
// Author: github.com/lzl124631x
// Time:
// O(MN) for SubrectangleQueries
// O(1) for updateSubrectangle
// O(Update) for getValue
// Space: O(MN)
class SubrectangleQueries {
vector<vector<int>> A, sub;
public:
SubrectangleQueries(vector<vector<int>>& rectangle) : A(rectangle) {}
void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
sub.push_back({ row1, col1, row2, col2, newValue });
}
int getValue(int row, int col) {
for (int i = sub.size() - 1; i >= 0; --i) {
if (row >= sub[i][0] && row <= sub[i][2] && col >= sub[i][1] && col <= sub[i][3]) return sub[i][4];
}
return A[row][col];
}
};