fork download
  1. // pre[i][j] - sum of all values which are above it or left of it
  2. vector<vector<int>> pre;
  3.  
  4. void pre2d(vector<vector<int>> &arr)
  5. {
  6. int r = arr.size(), c = arr[0].size();
  7. pre.resize(r, vi(c));
  8. pre[0][0] = arr[0][0];
  9.  
  10. // Base cases
  11. for (int i = 1; i < c; i++)
  12. pre[0][i] = pre[0][i - 1] + arr[0][i];
  13.  
  14. for (int i = 1; i < r; i++)
  15. pre[i][0] = pre[i - 1][0] + arr[i][0];
  16.  
  17. for (int i = 1; i < r; i++)
  18. {
  19. for (int j = 1; j < c; j++)
  20. pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + arr[i][j];
  21. }
  22. }
  23.  
  24. // returns value of submatrix with start coordinates as {sx, sy}
  25. int val(int sx, int sy, int ex, int ey)
  26. {
  27. if (!sy && !sx)
  28. return pre[ex][ey];
  29.  
  30. if (!sy)
  31. return pre[ex][ey] - pre[sx - 1][ey];
  32.  
  33. if (!sx)
  34. return pre[ex][ey] - pre[ex][sy - 1];
  35.  
  36. return pre[ex][ey] - pre[ex][sy - 1] - (pre[sx - 1][ey] - pre[sx - 1][sy - 1]);
  37. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:1: error: ‘vector’ does not name a type
 vector<vector<int>> pre;
 ^~~~~~
prog.cpp:4:12: error: variable or field ‘pre2d’ declared void
 void pre2d(vector<vector<int>> &arr)
            ^~~~~~
prog.cpp:4:12: error: ‘vector’ was not declared in this scope
prog.cpp:4:19: error: ‘vector’ was not declared in this scope
 void pre2d(vector<vector<int>> &arr)
                   ^~~~~~
prog.cpp:4:26: error: expected primary-expression before ‘int’
 void pre2d(vector<vector<int>> &arr)
                          ^~~
prog.cpp: In function ‘int val(int, int, int, int)’:
prog.cpp:28:16: error: ‘pre’ was not declared in this scope
         return pre[ex][ey];
                ^~~
prog.cpp:31:16: error: ‘pre’ was not declared in this scope
         return pre[ex][ey] - pre[sx - 1][ey];
                ^~~
prog.cpp:34:16: error: ‘pre’ was not declared in this scope
         return pre[ex][ey] - pre[ex][sy - 1];
                ^~~
prog.cpp:36:12: error: ‘pre’ was not declared in this scope
     return pre[ex][ey] - pre[ex][sy - 1] - (pre[sx - 1][ey] - pre[sx - 1][sy - 1]);
            ^~~
stdout
Standard output is empty