【统计子矩阵——部分前缀和+双指针】

题目

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 510;
int s[N][N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n, m, k;
    cin >> n >> m >> k;
    
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            s[i][j] += s[i-1][j];
        }
    
    ll ans =  0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = i; j <= n; j++)
        {
            for(int l = 1, r = 1, sum = 0; r <= m; r++)
            {
                sum += s[j][r] - s[i-1][r];
                while(sum > k)
                {
                    sum -= s[j][l] - s[i-1][l];
                    l++;
                }
                
                ans += r - l + 1;
            }
        }
    }
    
    cout << ans;
}
相关推荐
大鱼>2 分钟前
AI+快递分拣:视觉识别+自动分拣+异常检测
人工智能·深度学习·算法·机器学习
想要成为糕糕手5 分钟前
238. 除了自身以外数组的乘积 — 面试向深度解析
javascript·算法·面试
浩瀚地学16 分钟前
【面试算法笔记】0105-数组-螺旋矩阵
java·开发语言·笔记·算法·面试
Hesionberger1 小时前
动态规划与二分法破解最长递增子序列
java·数据结构·python·算法·leetcode
imuliuliang1 小时前
关于Kruskal 算法在图优化问题中的扩展应用7
算法
老约家的可汗1 小时前
Linux中基础IO
linux·服务器·算法
满怀冰雪2 小时前
第29篇-状态压缩DP-当状态很多时如何降维优化
java·算法·动态规划
令狐掌门2 小时前
2026华为OD面试题020:日志文件异常检测
算法·leetcode·华为od
炸薯条!2 小时前
从零开始学C++(4) --类和对象
开发语言·c++·算法
haluhalu.2 小时前
prompts.chat:07-few-shot-prompting
人工智能·算法