蓝桥杯省赛无忧 第二章 基础算法 课件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;
}


相关推荐
稚南城才子,乌衣巷风流22 分钟前
ST 表(Sparse Table)算法详解:原理、实现与应用
算法
hold?fish:palm28 分钟前
9 找到字符串中所有字母异位词
c++·算法·leetcode
Sw1zzle1 小时前
算法入门(六):贪心算法 - 基础入门(Leetcode 121/455/860/376/738)
算法·leetcode·贪心算法
青山木1 小时前
Hot 100 --- 岛屿数量
java·数据结构·算法·leetcode·深度优先·广度优先
不会就选b1 小时前
算法日常・每日刷题--<归并排序>1
数据结构·算法
危桥带雨2 小时前
排序算法(快排、归并、计数、基数排序)
数据结构·算法·排序算法
啦啦啦啦啦zzzz2 小时前
算法:回溯算法
c++·算法·leetcode
IT探索2 小时前
Linux 查找文件指令总结
linux·算法
Lzg_na2 小时前
订阅发布模块事例
c++