2536.cpp (831B)
1 class Solution { 2 public: 3 vector<vector<int>> rangeAddQueries(int n, const vector<vector<int>> &queries) const { 4 vector<vector<int>> mat(n, vector(n, 0)); 5 6 for (const auto &query : queries) { 7 mat[query[0]][query[1]]++; 8 if (query[2] + 1 < n && query[3] + 1 < n) mat[query[2] + 1][query[3] + 1]++; 9 if (query[2] + 1 < n) mat[query[2] + 1][query[1]]--; 10 if (query[3] + 1 < n) mat[query[0]][query[3] + 1]--; 11 } 12 13 for (int i = 0; i < n; i++) { 14 for (int j = 0, acc = 0; j < n; j++) { 15 mat[i][j] = acc += mat[i][j]; 16 } 17 } 18 19 for (int j = 0; j < n; j++) { 20 for (int i = 0, acc = 0; i < n; i++) { 21 mat[i][j] = acc += mat[i][j]; 22 } 23 } 24 25 return mat; 26 } 27 };