蓝桥OJ3291区间更新

 进行区间更新之后 必须要用前缀和还原才能得到原数组

蓝桥OJ3291区间更新_第1张图片 

#include
using namespace std;
const int N = 1e5 + 3;
int a[N],diff[N];

void solve(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];
    
    int x, y, z;
    while(m--)
    {
        cin >> x >> y >> z;
        diff[x] += z;
        diff[y+1] -= z;
    }
    
    //前缀和恢复
    for (int i = 1; i <= n; i++) a[i] = diff[i] + a[i-1];
    for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n];
    
}
int main()
{
    int n, m;
    while(cin >> n >> m) solve(n,m);
    return 0;
}

你可能感兴趣的:(蓝桥杯备赛练习,算法,c++)