Skip to content

Instantly share code, notes, and snippets.

@musou1500
Created August 23, 2020 15:57
Show Gist options
  • Save musou1500/9dfa655da00bff933d950de18de1c3e3 to your computer and use it in GitHub Desktop.
Save musou1500/9dfa655da00bff933d950de18de1c3e3 to your computer and use it in GitHub Desktop.
vector<vector<int>> Dim2PartialSum(vector<vector<int>> &mat, int w, int h) {
vector<vector<int>> psum(h + 1, vector<int>(w + 1, 0));
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
psum[i + 1][j + 1] =
psum[i + 1][j] + psum[i][j + 1] + mat[i][j] - psum[i][j];
}
}
return psum;
}
int Dim2PartialSumArea(vector<vector<int>> &psum, int x, int y, int w, int h) {
return psum[y + h][x + w] - psum[y + h][x] - psum[y][x + w] + psum[y][x];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment