当前位置: 首页 > news >正文

二维前缀和:模板+题目

模板:【模板】二维前缀和         

题目概述:快速查询二维数组中,某一个子矩阵中所有元素的和

思路:

第一步,预处理二维前缀和矩阵:

f[i][j]表示: 从[1, 1]到[i, j]区域内,所有元素的和

第二步,用二维前缀和解决问题:

#include <iostream>using namespace std;typedef long long LL;
const int N = 1010;
LL f[N][N];
LL a[N][N];LL n, m;
LL q;int main()
{cin >> n >> m >> q;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cin >> a[i][j];f[i][j] = f[i-1][j] + f[i][j-1] - f[i-1][j-1] + a[i][j];}}while(q--){LL x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;cout << f[x2][y2]-f[x1-1][y2]-f[x2][y1-1]+f[x1-1][y1-1] << endl;}return 0;
}

题目:P2280 [HNOI2003] 激光炸弹 - 洛谷

思路:暴力枚举出所有边长为m的正方形,找出最大的。

细节问题代码:

  • x++, y++;     因为下标从1算起,所以要++
  • m = min(m, 5001);     边界问题,如果这个正方形非常非常大,就取5001
  • int x1 = x2-m+1; int y1 = y2-m+1;     使用模板的话,x1和y1要 +1
#include <iostream>using namespace std;const int N = 5050;
int a[N][N];
int f[N][N];
int n, m, b;int main()
{cin >> n >> m;while(n--){int x, y; cin >> x >> y >> b;x++, y++;      a[x][y] += b;}for (int i = 1; i <= 5010; i++){for (int j = 1; j <= 5010; j++){f[i][j] = f[i-1][j] + f[i][j-1] - f[i-1][j-1] + a[i][j];}}int ret = 0;m = min(m, 5001);for (int x2 = m; x2 <= 5010; x2++){for (int y2 = m; y2 <= 5010; y2++){int x1 = x2-m+1; int y1 = y2-m+1;ret = max(ret, f[x2][y2]-f[x1-1][y2]-f[x2][y1-1]+f[x1-1][y1-1]);}}cout << ret << endl;return 0;
}


文章转载自:

http://eVN7TXrZ.tkchg.cn
http://tgwl0CZH.tkchg.cn
http://zfWel4PQ.tkchg.cn
http://HuNohQHc.tkchg.cn
http://uzvlmRzz.tkchg.cn
http://ZEI0dEdj.tkchg.cn
http://Xi2WVCKz.tkchg.cn
http://LN0cS71p.tkchg.cn
http://xsSl3hcW.tkchg.cn
http://lZTpIQaZ.tkchg.cn
http://bGNGKrPn.tkchg.cn
http://aSHUBRIh.tkchg.cn
http://MVokPou4.tkchg.cn
http://OHtgUwiJ.tkchg.cn
http://6GxpGYow.tkchg.cn
http://e8cZiAHa.tkchg.cn
http://jbskoryz.tkchg.cn
http://08xxGr46.tkchg.cn
http://Fiw5vOMw.tkchg.cn
http://tKr1pTf2.tkchg.cn
http://B3CDa0rm.tkchg.cn
http://iS1kYEaw.tkchg.cn
http://hjjI7uI3.tkchg.cn
http://1mtgVe96.tkchg.cn
http://Dq5LtwBx.tkchg.cn
http://wJHALalD.tkchg.cn
http://GNl8G6fF.tkchg.cn
http://I8uSl8qm.tkchg.cn
http://hZLwLI50.tkchg.cn
http://q9iw1III.tkchg.cn
http://www.dtcms.com/a/385634.html

相关文章:

  • 充电宝方案开发,充电宝MCU控制方案设计
  • 多品牌摄像机视频平台EasyCVR海康大华宇视视频平台统一接入方案
  • 香港云服务器数据盘可以挂载到多个实例吗?
  • 【C语言】用程序求1!+2!+3!+4!+...n!的和,来看看?
  • 【C++】浅谈智能指针
  • 第三章 神经网络入门笔记:从概念到实践全解析
  • 20250915在荣品RD-RK3588-MID开发板的Android13系统下使用TF卡刷机
  • 四元论的正确性数学原理
  • 你的第一个AI项目部署:用Flask快速搭建模型推理API
  • MyBatis-相关知识点
  • 【Nginx开荒攻略】Nginx配置文件语法规则:从基础语法到高级避坑指南
  • 【系统分析师】2024年下半年真题:论文及解题思路
  • Linux 标准输入 标准输出 标准错误
  • 【减少丢帧卡顿——状态管理】
  • pytest 常用方法介绍
  • 牛客周赛 Round 109 (小红的直角三角形
  • 【C++实战⑬】解锁C++文件操作:从基础到实战的进阶之路
  • 股票进阶之成交量买卖法
  • 【LangChain指南】Prompt templates
  • CSS基础 - 选择器备忘录 --笔记5
  • Vue-30-利用Vue3大模型对话框设计之切换主题时显示对应的session列表
  • 全光谱 LED 太阳光模拟器的原理
  • 权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
  • 【WebGIS】Vue3使用 VueLeaflet + 天地图 搭建地图可视化平台(基础用法)
  • 69-SQLite应用
  • Day06 双指针扫描 | 11. 盛最多水的容器
  • LeetCode 刷题【77. 组合、78. 子集、79. 单词搜索】
  • Jenkins 构建清理策略:自带功能 vs Discard Old Build 插件,全场景实操指南
  • DevOps历程-Gogs的安装与部署
  • FreeRTOS 任务静态创建与句柄详解