蓝桥杯省赛无忧 第二章 基础算法 课件27 差分

01 差分的原理和特点


02 差分的实现

03 例题讲解

csharp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N  = 1e5 +7;
int arr[N],diff[N];
void solve(int n,int m){
  for(int i=1;i<=n;i++) cin >> arr[i];
  for(int i=1;i<=n;i++) diff[i] = arr[i] - arr[i-1];
  while(m--){
    int x,y,z;cin >> x >> y >> z;
    diff[x] += z,diff[y+1] -= z;
  }
  for(int i=1;i<=n;i++) arr[i] = arr[i-1] + diff[i];
  for(int i=1;i<=n;i++) cout << arr[i] << ' ';
  cout << endl;
}
int main()
{
 int n,m;
 while(cin >> n >> m) solve(n,m);
  return 0;
}


相关推荐
不会就选b3 小时前
算法日常・每日刷题--<贪心>14
算法
初願致夕霞4 小时前
C++11:类型推导与完美转发复盘笔记
c++
小猪写代码4 小时前
C++中什么是类,什么是对象
c++
mmmmath_36 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z6 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
醇氧7 小时前
MySQL 8.0 系统表损坏与引擎转换故障排查实战
数据结构·算法
大熊背7 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)
算法·白平衡·色度·色温
钓鱼的肝8 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
HZZD_HZZD8 小时前
CSDN_批发市场水电漏损归因算法LAM的原理与落地
嵌入式硬件·物联网·算法