离散化,前缀和,差分
离散化,前缀和,差分
一维前缀和和差分之前学过不再记录
二维情况
前缀和
多维前缀和的普通求解方法几乎都是基于容斥原理
例如有这样一个矩阵,可以视为二维数组:
1 2 4 3
5 1 2 4
6 3 5 9
定义一个矩阵
1 3 7 10
6 9 15 22
12 18 29 45
第一个问题是递推求sum的过程,
因为同时加了
第二个问题是如何应用,譬如求
答案
差分
由sum逆推出来,即上述等式改为
例题
// Problem: Air Cownditioning
// Contest: Virtual Judge - USACO
// URL: https://vjudge.net/problem/USACO-1156
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <array>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
typedef long long LL;
const int N = 1e5+10;
int n,a[N],b[N];
LL ans;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
b[i]-=a[i];
}
for(int i=1;i<=n+1;i++) ans+=max(b[i]-b[i-1],0);
printf("%lld\n",ans);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现