// Online C++ compiler to run C++ program online #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n , m , q ; cin >> n >> m >> q ; long long a[n+1][m+1] , f2[n+1][m+1] = {0} , f[n+1][m+1] = {0} ; for ( int i = 1 ; i <= n ; i ++) { for (int j = 1 ; j <=m ; j ++) { cin >>a[i][j] ; if ( j != 1 ) f[i][j] = a[i][j] + f[i][j-1] ; else f[i][j] = a[i][j] ; cout <<f[i][j]<<" " ; } cout <<"\n" ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++) { if ( j != 1 ) f2[j][i] = a[j][i] + f2[j-1][i] ; else f2[j][i] = a[j][i] ; cout <<f2[j][i]<<" " ; } cout <<"\n" ; } while (q -- ) { int x , y , x1 , y1 , sum = 0 ; cin >> x >> y >> x1 >> y1; for ( int col = x ; col <= x1 ; col ++ ) { if ( y == 1 ) sum += f[col ][y1 ] ; else sum += f[col ][y1 ] - f[col ][y ] + a[col ][y ]; } cout <<sum <<"\n" ; } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter