原题链接
定义差分数组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;
}
标签:797,原题,int,更改,差分,数组
From: https://www.cnblogs.com/StkOvflow/p/16994910.html