1923. 二维前缀和

模板题,不解释代码及一般步骤。

#include <bits/stdc++.h> using namespace std; using ll = long long; signed main() { ios::sync_with_stdio(false), cin.tie(0); ll n, m, t; cin >> n >> m >> t; vector<vector<ll>> A(n + 1, vector<ll>(m + 1, 0)); for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { cin >> A[i][j]; A[i][j] += A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1]; } } for (ll a, b, c, d, i = 1; i <= t; ++i) { cin >> a >> b >> c >> d; cout << (A[b][d] - A[a - 1][d] - A[b][c - 1] + A[a - 1][c - 1]) << '\n'; } return 0; }