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;
}