AcWing 797. 差分 C++
原题链接
简单
作者:
沙漠绿洲
,
2020-08-19 16:06:14
,
所有人可见
,
阅读 378
C++ 代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
vector<int> ans(n + 1);
for (int i = 1; i <= n; ++i) cin >> ans[i];
//求差分
vector<int> cf(n + 1, 0);
for (int i = 1; i <= n; ++i) cf[i] = ans[i] - ans[i - 1];
while (m--) {
int l, r, c;
cin >> l >> r >> c;
cf[l] += c, cf[r + 1] -= c;
}
//求前缀和还原原序列
for (int i = 1; i <= n; ++i) {
cf[i] += cf[i - 1];
cout << cf[i] << " ";
}
cout << endl;
return 0;
}