差分运算(使用C++匿名函数,让代码更简洁。)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m; cin >> n >> m;
vector<int> nums(n+2, 0);
auto insert = [&](int l, int r, int c) {
nums[l] += c; nums[r+1] -= c;
};
for (int i = 1; i <= n; ++i) {
int t; cin >> t;
insert(i, i, t);
}
for (int i = 1; i <= m; ++i) {
int l, r, c; cin >> l >> r >> c;
insert(l, r, c);
}
for (int i = 1; i <= n; ++i) {
nums[i] += nums[i-1];
cout << nums[i] << ' ';
}
return 0;
}