AcWing 797. 差分

Posted MangataTS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AcWing 797. 差分相关的知识,希望对你有一定的参考价值。

题目连接

https://www.acwing.com/problem/content/799/

思路

裸差分,我们通过相邻两数之差就能获取差分数组即 d [ i ] = a [ i ] − a [ i − 1 ] d[i]=a[i]-a[i-1] d[i]=a[i]a[i1],我们修改i这个位置的值,比如说给他加上一个x,实际上就是在我们求前缀和的时候,之后的所有数都加上了一个x,所以我们区间修改是这样的a[L]+x,a[R+1]-x

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000009
#define endl "\\n"
#define PII pair<int,int>
ll ksm(ll a,ll b) 
	ll ans = 1;
	for(;b;b>>=1LL) 
		if(b & 1) ans = ans * a % mod;
		a = a * a % mod;
	
	return ans;


ll lowbit(ll x)return -x & x;

const int N = 2e6+10;
int n,a[N],d[N];

int main()

	int m;
	cin>>n>>m;
	for(int i = 1;i <= n; ++i) 
		cin>>a[i];
		d[i] = a[i] - a[i-1];
	
	while(m--) 
		int l,r,c;
		cin>>l>>r>>c;
		d[l] += c;
		d[r+1] -= c;
	
	ll res = 0;
	for(int i = 1;i <= n; ++i) 
		res += d[i];
		cout<<res<<" \\n"[i==n];
	
	
	return 0;

以上是关于AcWing 797. 差分的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥杯AcWing 题目题解 - 二分与前缀和差分

AcWing 797. 差分

第五讲 树状数组与线段树 未完结

797. 差分

前缀和和差分模板(AcWing 795-798)

AcWing 795. 前缀和