题解 | #【模板】差分#
#include <bits/stdc++.h> using namespace std; int n, m; int main() { cin >> n >> m; vector<long long> dp(n + 1); vector<int> a(n + 10); for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j < m; j++) { int l, r, k; cin >> l >> r >> k; dp[l - 1] += k; //题目数组索引从 1开始,所以-1, dp[r] -= k; } long long ans = 0; for (int i = 0; i < n; i++) { ans += dp[i]; cout << ans + a[i] << " "; } return 0; }
算法常用解题技巧 文章被收录于专栏
算法常用解题技巧