Loading... ### 对$S[i][j]$的理解 ![](https://blog.fivk.cn/usr/uploads/2021/11/2040493569.png) $S[i][j]$即为图红框中所有数的的和为: $S[i][j]=S[i][j−1]+S[i−1][j]−S[i−1][j−1]+a[i,j]$ ### 二维区间求和 ![](https://blog.fivk.cn/usr/uploads/2021/11/2022973368.png) $(x1,y1),(x2,y2)$这一子矩阵中的所有数之和为: $S[x2][y2]−S[x1−1][y2]−S[x2][y1−1]+S[x1−1][y1−1]$ ```c #include <iostream> using namespace std; const int N = 1010; int a[N][N], s[N][N]; int main() { int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf("%d", &a[i][j]); s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + a[i][j]; // 求前缀和 } int x1,y1,x2,y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); // 算子矩阵的和 printf("%d\n", s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]); return 0; } ``` 最后修改:2021 年 11 月 20 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏