【差分】TT's Magic Cat

题面

大致意思是有多个区间修改操作,对[l,r]内的数加上或减去c。

Input

在这里插入图片描述

Output

;

Examples

Input

4 2
-3 6 8 4
4 4 -2
3 3 1

Output

-3 6 9 2

Input

2 1
5 -2
1 2 4

Output

9 2

Input

1 2
0
1 1 -8
1 1 -6

Output

-14

思路

只修改不查询或者修改和查询分开,用差分。差分只记录整个过程中修改值在何处变化,比如[l,r]+c,修改值在l处+c,在r+1处-c(r+1<=n+1,所以多开一点空间)。
差分是一种思想,在有些时候有妙用。

代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
typedef long long LL; 
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define mem(a,s) memset(a,s,sizeof(a))
int n,q;
LL a[200010];
LL b[200010];
int main(){
	//freopen("1.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin>>n>>q;
	mem(b,0);
	rep(i,1,n)
		cin>>a[i];
	rep(i,1,q){
		int l,r,x;
		cin>>l>>r>>x;
		b[l]+=x;
		b[r+1]-=x;
	}
	LL x=0;
	rep(i,1,n){
		x+=b[i];
		cout<<a[i]+x;
		if(i<n)
			cout<<' ';
		else cout<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值