AcWing 797. 差分打卡
原题链接
简单
作者:
午间小憩
,
2021-07-20 21:54:28
,
所有人可见
,
阅读 225
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6 + 1;
int a[N], b[N];
int n, m;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &a[i]);
b[i] = a[i] - a[i - 1]; //算出差分数组b
}
int l, r, c;
while (m--)
{
scanf("%d%d%d", &l, &r, &c);
b[l] += c; //搞定边界
b[r + 1] -= c; //最终答案是b[l] == 原来的b[l] + c
}
//前缀和运算
for (int i = 1; i <= n; i++)
{
a[i] = a[i - 1] + b[i]; //重新构造前缀和数组a,此时的b已经加了b[l]已经加上一个c了
printf("%d ", a[i]);
}
return 0;
}