lanqiao 2109 统计子矩阵(一维前缀和+滑动窗口)

This commit is contained in:
2025-04-01 19:38:39 +08:00
parent 90023c8e50
commit 07a5be69f1

38
13lanqiao/test6-2.cpp Normal file
View File

@@ -0,0 +1,38 @@
// lanqiao 2109 统计子矩阵(一维前缀和+滑动窗口)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e2 + 10;
int n, m, k;
int a[N][N], sum[N][N];
signed main(){
cin >> n >> m >> k;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
sum[i][j] = sum[i-1][j] + a[i][j];
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int i1 = i; i1 <= n; i1++){
int l = 1, r, s = 0;
for(r = 1; r <= m; r++){
s += sum[i1][r] - sum[i-1][r];
while(s > k){
s -= sum[i1][l] - sum[i-1][l];
l++;
}
ans += r - l + 1;
}
}
}
cout << ans << endl;
return 0;
}
/* samples -> 19
3 4 10
1 2 3 4
5 6 7 8
9 10 11 12
*/