sum colm and row
Tue Feb 28 2023 17:07:52 GMT+0000 (Coordinated Universal Time)
Saved by
@soso
// 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;
}
content_copyCOPY
Comments