fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int a[1005][1005];
  5. int pre[1005][1005];
  6.  
  7. int main(){
  8. int n, m; cin >> n >> m;
  9. for(int i = 1; i <= n; i++){
  10. for(int j = 1; j <= m; j++){
  11. cin >> a[i][j];
  12. }
  13. }
  14.  
  15. for(int i = 1; i <= n; i++){
  16. for(int j = 1; j <= m; j++){
  17. pre[i][j] = pre[i][j - 1] + pre[i - 1][j] - pre[i - 1][j - 1] + a[i][j];
  18. }
  19. }
  20.  
  21. for(int i = 1; i <= n; i++){
  22. for(int j = 1; j <= m; j++){
  23. cout << pre[i][j] << " ";
  24. }
  25. cout << endl;
  26. }
  27. int q; cin >> q;
  28. while(q--){
  29. int x1, x2, y1, y2;
  30. cin >> x1 >> x2 >> y1 >> y2;
  31. int res = pre[x2][y2] - pre[x2][y1 - 1] - pre[x1 - 1][y2] + pre[x1 - 1][y1 - 1];
  32. cout << res << endl;
  33. }
  34. }
Success #stdin #stdout 0.01s 5552KB
stdin
8 8
1 1 0 0 0 1 1 1 
1 0 0 0 1 1 1 1 
0 1 1 1 0 0 1 0 
1 1 1 1 0 1 1 0 
1 1 1 1 1 0 1 1 
1 0 0 1 0 1 0 1 
0 0 0 0 1 0 1 0 
1 1 0 0 0 1 0 1 
3
2 3 1 7
2 2 2 7
1 2 1 8
stdout
1 2 2 2 2 3 4 5 
2 3 3 3 4 6 8 10 
2 4 5 6 7 9 12 14 
3 6 8 10 11 14 18 20 
4 8 11 14 16 19 24 27 
5 9 12 16 18 22 27 31 
5 9 12 16 19 23 29 33 
6 11 14 18 21 26 32 37 
8
3
10