B-TT‘s Magic Cat (差分数组)

B-TT‘s Magic Cat (差分数组)

一、题目描述

Thanks to everyone’s help last week, TT finally got a cute cat. But what TT didn’t expect is that this is a magic cat.

One day, the magic cat decided to investigate TT’s ability by giving a problem to him. That is select n cities from the world map, and a[i] represents the asset value owned by the i-th city.

Then the magic cat will perform several operations. Each turn is to choose the city in the interval [l,r] and increase their asset value by c. And finally, it is required to give the asset value of each city after q operations.

Could you help TT find the answer?

Input

The first line contains two integers n,q(1≤n,q≤2⋅105)
— the number of cities and operations.
The second line contains elements of the sequence a:
integer numbers a1,a2,...,an (−106≤ai≤106).
Then q lines follow, each line represents an operation.
The i-th line contains three integers l,r 
and c (1≤l≤r≤n,−105≤c≤105) for the i-th operation.

Output

Print n integers a1,a2,…,an one per line, 
and ai should be equal to the final asset value of the i-th city.

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

二、思路与算法

本题核心为差分数组b[i]的实现和使用。
b[0]=a[0];
b[i]=a[i]-a[i-1]; //(i>0)

根据定义可以看到,i不为0时,a[i]=差分数组b[i]前i项和。
而想对a数组区间[l,r]每个元素都做+c操作时,差分数组只需要给b[l]加c,再给b[r+1]减c。

所以,本题只需要每次操作中对对应b[i]操作,最后按序输出b的0、1、2…n-1项和即可。

三、代码实现

#include<cstdio>
using namespace std;

int n=0,q=0;

int main(){
	scanf("%d",&n);
	scanf("%d",&q);
	long long a[n]={0};
	long long b[n]={0};//差分数组 
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}//a数组赋值 
	b[0]=a[0];
	for(int i=1;i<n;i++){
		b[i]=a[i]-a[i-1];
	}//b数组赋值 
	
	for(int i=0;i<q;i++){
		long long l=0,r=0,c=0;
		scanf("%lld",&l);		scanf("%lld",&r);		scanf("%lld",&c);
		b[l-1]+=c;	b[r]-=c;
	}//q次操作 
	
	long long t=b[0];
	printf("%lld",b[0]);
	for(int i=1;i<n;i++){
		t=t+b[i];
		printf(" %lld",t);
	}
	//输出所有值 
	
	return 0;
} 

四、经验与总结

  1. 注意如果需要把变量都设置为long long类型,那么scanf输入、printf输出都要%lld!如果scanf输入时忘记%lld,就算输出时%lld也没有用!(并且这样写并不会有编译问题,不注意可能会遗漏)
  2. 根据题目数据规模,需要用long long类型时一定要用!不要想当然上int,最后会导致WA!
  3. 差分数组总体来说比较简单,本题的实现也较为简单,只要根据输入的a[i]求出对应的差分数组,再进行操作,最后输出,就可以得到正确的答案。
  4. 差分数组明显可以降低对某区间内数据做统一操作的时间复杂度,从遍历某段区间,到O(1),整体复杂度也大大降低。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JB-TT-JBF-11SF是一份说明书,用于介绍有关特定产品的详细信息。该产品属于JB-TT-JBF系列的第11个型号,而“SF”可能代表着产品的某些特殊功能或设计。 该说明书主要包括以下内容: 1. 产品概述:介绍该产品的基本信息,包括产品名称、型号、用途等。 2. 技术参数:列举产品的技术参数,如尺寸、重量、额定电压、额定功率等,这些参数对用户选择和使用产品具有重要意义。 3. 结构和组成:详细描述产品的结构和各个组件的组成,以便用户了解产品的构造和组织。 4. 使用方法:说明产品的正确使用方法,包括安装、操作、调整和维护等。此部分对于用户正确操作产品、延长使用寿命非常重要。 5. 安全注意事项:列出用户在使用过程中需要注意的安全事项,以避免可能发生的意外伤害或设备损坏。 6. 故障排除:提供一些常见故障和解决方案,以帮助用户在遇到问题时尽快解决。 7. 维护保养:对产品的维护保养提供指导,包括清洁、润滑、存放等。正确的维护保养可以延长产品寿命和保证其正常运行。 最后,该说明书可能还包括配件清单、售后服务等相关信息,以提供全面的帮助和支持。 总之,JB-TT-JBF-11SF说明书是一份为用户提供产品详细信息、使用方法和安全注意事项的文件。通过阅读该说明书,用户可以更好地了解、选择和使用该产品。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值