- [洛谷 U53525 前缀和(例题)](https://www.luogu.org/problemnew/show/U53525)
- [洛谷 U69096 前缀和的逆](https://www.luogu.org/problemnew/show/U69096)
- [AT2412 最大の和](https://www.luogu.org/problemnew/show/AT2412)
-- [洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens](https://www.luogu.org/problemnew/show/P3131)
+- [洛谷 P3131 \[USACO16JAN\] 子共七 Subsequences Summing to Sevens](https://www.luogu.org/problemnew/show/P3131)
### 参考
第二个问题就是如何应用,譬如求 $(x1,y1) - (x2,y2)$ 子矩阵的和。
那么,根据类似的思考过程,易得答案为 $sum_{x2,y2} - sum_{x1 - 1,y2} - sum_{x2,y1 - 1} + sum_{x1 - 1,y1 - 1}$ 。
-下面给出 [洛谷 P1387 最大正方形](https://www.luogu.org/problemnew/show/P1387) 这道题目的参考程序来帮助大家理解二维前缀和。
+下面给出[洛谷 P1387 最大正方形](https://www.luogu.org/problemnew/show/P1387)这道题目的参考程序来帮助大家理解二维前缀和。
```cpp
-#include <iostream>
#include <algorithm>
+#include <iostream>
using namespace std;
int a[103][103];
-int b[103][103]; // 前缀和数组,相当于上文的 sum[]
-int main()
-{
- int n, m;
- cin >> n >> m;
-
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= m; j++)
- {
- cin >> a[i][j];
- b[i][j] = b[i][j-1] + b[i-1][j] - b[i-1][j-1] + a[i][j]; // 求前缀和
- }
+int b[103][103]; // 前缀和数组,相当于上文的 sum[]
+int main() {
+ int n, m;
+ cin >> n >> m;
+
+ for (int i = 1; i <= n; i++) {
+ for (int j = 1; j <= m; j++) {
+ cin >> a[i][j];
+ b[i][j] =
+ b[i][j - 1] + b[i - 1][j] - b[i - 1][j - 1] + a[i][j]; // 求前缀和
}
+ }
- int ans = 1;
-
- int l = 2;
- while (l <= min(n, m))
- {
- for (int i = l; i <= n; i++)
- {
- for (int j = l; j <= m; j++)
- {
- if (b[i][j] - b[i-l][j] - b[i][j-l] + b[i-l][j-l] == l*l)
- {
- ans = max(ans, l);
- }
- }
+ int ans = 1;
+
+ int l = 2;
+ while (l <= min(n, m)) {
+ for (int i = l; i <= n; i++) {
+ for (int j = l; j <= m; j++) {
+ if (b[i][j] - b[i - l][j] - b[i][j - l] + b[i - l][j - l] == l * l) {
+ ans = max(ans, l);
}
- l++;
+ }
}
-
- cout << ans << endl;
- return 0;
+ l++;
+ }
+
+ cout << ans << endl;
+ return 0;
}
```