Week5 作业 B - TT's Magic Cat Gym - 272542A 前缀和与差分

题目

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⋅10^5) — the number of cities and operations.
The second line contains elements of the sequence a: integer numbers a1,a2,…,an (−10^6 ≤ai≤ 10^6).
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
解题思路

本题是给出一个数组以及若干个操作,每个操作在给出的区间内对数组进行相同的数据加减操作。数组的长度和总操作数最大为 2*10^5 ,而且每个操作区间范围最大为 10^6 ,如果使用常规方法对每个操作逐个处理数组元素,效率是比较低的。因此我们用差分构造方式处理数据。每获得一种操作,在 op 中对应起始坐标加上操作数,在结束坐标减去当前操作数。最后求最终值时,将原数组元素的值加上 op 数组中当前坐标以及前面的值的累加值也就是 op 数组的前缀和,就是经过处理后的最终值。

本题需要注意数据大小,操作数在 −10^6 ≤ai≤ 10^6 范围内,需要使用 long long 处理操作数的累加值。

程序源码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
using namespace std;

int main() {
	int n, q, v1 = 0, v2 = 0, v3 = 0;
	scanf("%d%d", &n, &q); //给出城市数n和操作数q
	int* a = new int[n + 2]{ 0 }; //建立城市值的数组a
	long long* op = new long long[n + 2]{ 0 }; //建立操作数数组op
	for (int i = 1; i <= n; i++) { //获取每个城市的数值
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < q; i++) { //获取操作数据
		scanf("%d%d%d", &v1, &v2, &v3); //v1起始 v2终点 v3操作数
		op[v1] += v3; //起始坐标处加上操作数
		op[v2 + 1] += 0 - v3; //终点坐标处减去操作数
	}
	long long ct = 0; //当前操作数的累加值
	for (int i = 1; i <= n; i++) { //开始操作数数组
		ct += op[i]; //加上当前操作数
		if (i == n) {
			printf("%lld\n", a[n] + ct); //最后一个元素,输出换行
		}
		else printf("%lld ", a[i] + ct); //输出原数值加上操作数累加值,输出空格
	}
	// i==n
	return 0; //结束
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值