题目描述:
解题思路:
一段连续区间加减,采用差分。最终每个元素结果与0比较大小,比0小即负数输出0。
题解:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
ll a[N], diff[N];//题目元素大小需要开ll
int res(int n, int m)
{
for(int i = 1; i <= n; i++)cin >> a[i];
for(int i = 1; i <= n; i++)diff[i] = a[i] - a[i-1];
while(m--)
{
int x, y, z; cin >> x >> y >> z;
diff[x] += z; diff[y + 1] -= z;
}
for(int i = 1; i <= n; i++)a[i] = a[i-1] + diff[i];
for(int i = 1; i <= n; i++)cout << max(0ll, a[i]) << " ";
//采用max()比if要简短。0ll即ll类型的0,因为max比较需要同类型;但不同类型可以相互运算
return 0;
}
int main()
{
int n, m;
cin >> n >> m;
res(n, m);
return 0;
}