使用差分数组维护区间修改即可
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e5 + 5; int __t = 1, n, m, l, r, k; int a[N], b[N]; void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < m; i++) { cin >> l >> r >> k; b[l] += k; if (r + 1 <= n) b[r + 1] -= k; } for (int i = 1; i <= n; i++) { if (i > 1) b[i] += b[i - 1]; a[i] += b[i]; } for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << "\n"; } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif // cin >> __t; while (__t--) solve(); return 0; }