week5 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≤2e5) — the number of cities and operations.

The second line contains elements of the sequence a: integer numbers a1,a2,…,an (−1e6≤ai≤1e6).

Then q lines follow, each line represents an operation. The i-th line contains three integers l,r and c (1≤l≤r≤n,−1e5≤c≤1e5) 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.

Example

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

思路

本题使用前缀和来进行解题
前缀和:(原数组为a,前缀和数组为b)
a[0]=b[0];
a[i+1]=a[i]+b[i+1];
对于[l,r]中的每个数都加上q,使用前缀和的话,仅仅需要将b[l]+=q, b[r+1] =b[r+1]-q 即可。(r<数组的最大角标,若r为最大角标,则省略后一步),将所有操作完成后,再使用前缀和将原数组进行更新即可

代码

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int n, q;
	scanf("%d %d", &n, &q);
	long long * a = new long long[n + 100];
	long long* b = new long long[n + 100];
	a[0] = 0;
	b[0] = 0;
	b[n + 1] = 0;
	for (int i = 1; i <= n; i++)
		scanf("%lld", &a[i]);
	for (int i = 1; i <= n; i++)
	{
		if (i == 1)
			b[i] = a[i];
		else
			b[i] = a[i] - a[i - 1];
	}
	for (int i = 0; i < q; i++)
	{
		int l, r, num;
		scanf("%d %d %d", &l, &r, &num);
		b[l] += num;
		b[r + 1] -= num;
	}
	for (int i = 1; i <= n; i++)
	{
		if (i == 1)
			a[i] = b[i];
		else
			a[i] = a[i - 1] + b[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cout << a[i];
		if (i < n)
			cout << " ";
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值