题解 | #【模板】二维前缀和#
【模板】二维前缀和
http://www.nowcoder.com/practice/99eb8040d116414ea3296467ce81cbbc
#include<iostream>
using namespace std;
long long int graph[1001][1001], sum[1001][1001] = { 0 };
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q, x1, x2, y1, y2;
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
cin >> graph[i][j];
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + graph[i][j] - sum[i - 1][j - 1];
}
while (q--) {
cin >> x1 >> y1 >> x2 >> y2;
cout << sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1] << endl;
}
}