StkOvflow

STACK OVERFLOW!

一言(ヒトコト)

797差分

原题链接
定义差分数组b[],其中\(b[i] = a[i] - a[i - 1]\)
\(a_{x} = \sum_{i=1}^{x}b_{i}\)
更改\(a[l~r]\), 只要更改\(b[l-1]\)\(b[r]\)即可, 最后要对\(b[]\)数组做一次前缀和得到之前的\(a[]\)

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
int n, a[N], b[N], m;

void insert(int l, int r, int c)
{
    b[l] += c;
    b[r + 1] -= c;
}

int main()
{
    scanf("%d%d",  &n,  &m);

    for(int i = 1;i <= n;i ++ )
    {
        scanf("%d",  &a[i]);
        b[i] = a[i] - a[i - 1];
    }
        

    while(m--)
    {
        int l, r, c;
        scanf("%d%d%d",  &l,  &r,  &c);
        insert(l,  r,  c);
    }

    for(int i = 1;i <= n;i ++ )
        b[i] += b[i - 1];

    for(int i = 1;i <= n;i ++ )
        printf("%d ", b[i]);

    return 0;
}
posted @ 2022-12-20 19:04  StkOvflow  阅读(20)  评论(0)    收藏  举报