题解 | 【模板】二维前缀和
【模板】二维前缀和
https://www.nowcoder.com/practice/99eb8040d116414ea3296467ce81cbbc
#include <iostream> using namespace std; const int maxn = 1005; typedef long long LL; LL sum[maxn][maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; int x; LL cur = 0; for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ cin >>x; cur += x; sum[i][j] = cur - (sum[i-1][m] - sum[i-1][j]); } } int x1,x2,y1,y2; for(int i=0; i<q; i++){ cin >> x1 >> y1 >> x2 >> y2; LL tmp1 = sum[x1-1][y2]; LL tmp2 = sum[x2][y1-1]; cout << sum[x2][y2] - tmp1 -tmp2 + sum[x1-1][y1-1] << endl; } } // 64 位输出请用 printf("%lld")