【bzoj2901】矩陣求和
呵呵呵呵呵呵呵呵呵呵呵
程式碼:
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int INF = 2147483647;
const int maxn = 2010;
int n,m;
int a[maxn][maxn],b[maxn][maxn];
LL ans;
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
int main()
{
#ifdef AMC
freopen("AMC1.txt","r",stdin);
freopen("AMC2.txt","w",stdout);
#endif
n = getint(); m = getint();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
a[i][j] = getint();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
b[i][j] = getint();
for (int j = 1; j <= n; j++)
for (int i = 1; i <= n; i++)
a[i][j] += a[i - 1][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
b[i][j] += b[i][j - 1];
for (int i = 1; i <= m; i++)
{
int x = getint(),y = getint();
int z = getint(),w = getint();
int xl = min(x,z),xr = max(x,z);
int yl = min(y,w),yr = max(y,w);
ans = 0;
for (int j = 1; j <= n; j++)
ans += 1ll * (a[xr][j] - a[xl - 1][j]) * (b[j][yr] - b[j][yl - 1]);
printf("%lld\n",ans);
}
return 0;
}