算法基础()

算法——差分https://www.acwing.com/problem/content/description/799/

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;
int a[N], b[N];
int main(){
	int n, m;
	cin >> n >> m;
	
	for(int i = 1; i <= n; i++){  //构造差分数组 
		cin >> a[i];
		b[i] = a[i] - a[i - 1];
	}
	
	int l, r, c;
	while(m--){
		cin >> l >> r >> c;
		b[l] += c;
		b[r + 1] -= c;
	}
	
	for(int i = 1; i <= n; i++){
		a[i] = b[i] + a[i - 1];
		cout << a[i] << " ";
	}

	
	return 0;
}

————————

算法——差分https://www.acwing.com/problem/content/description/799/

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;
int a[N], b[N];
int main(){
	int n, m;
	cin >> n >> m;
	
	for(int i = 1; i <= n; i++){  //构造差分数组 
		cin >> a[i];
		b[i] = a[i] - a[i - 1];
	}
	
	int l, r, c;
	while(m--){
		cin >> l >> r >> c;
		b[l] += c;
		b[r + 1] -= c;
	}
	
	for(int i = 1; i <= n; i++){
		a[i] = b[i] + a[i - 1];
		cout << a[i] << " ";
	}

	
	return 0;
}