week5作业B

题意:
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 nn cities from the world map, and a[i]a[i] represents the asset value owned by the ii-th city.
Then the magic cat will perform several operations. Each turn is to choose the city in the interval [l,r][l,r] and increase their asset value by cc. And finally, it is required to give the asset value of each city after qq operations.
Could you help TT find the answer?
input:
The first line contains two integers n,qn,q (1≤n,q≤2⋅105)(1≤n,q≤2·105) — the number of cities and operations.
The second line contains elements of the sequence aa: integer numbers a1,a2,…,ana1,a2,…,an (−106≤ai≤106)(−106≤ai≤106).
Then qq lines follow, each line represents an operation. The ii-th line contains three integers l,rl,r and cc (1≤l≤r≤n,−105≤c≤105)(1≤l≤r≤n,−105≤c≤105) for the ii-th operation.
output:
Print nn integers a1,a2,…,ana1,a2,…,an one per line, and aiai should be equal to the final asset value of the ii-th city.
sample input:
4 2
-3 6 8 4
4 4 -2
3 3 1
sample output :
-3 6 9 2
sample input :
2 1
5 -2
1 2 4
sample output:
9 2
sample input :
1 2
0
1 1 -8
1 1 -6
sample output:
-14
思路:
采用差分构造法,对于数组当q次操作,就相当于在差分构造的新数组中,数组b[l]加c,数组b[r+1]减c。在操作结束后就可以转换成原来数组输入的形式,输出结果。
代码:

#include<iostream>
#include<stdio.h>
using namespace std;
long long a[200050];
long long b[200050];
int l,r,c;
int n,q;
int main()
{
 cin.sync_with_stdio(false);
 cin>>n>>q;
	
	
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		//scanf("%d",&a[i]);
	}
	b[0]=a[0];
	for(int i=1;i<n;i++)
	{
		b[i]=a[i]-a[i-1];
	}
	for(int i=0;i<q;i++)
	{
		cin>>l>>r>>c;
		//scanf("%d%d%d",&l,&r,&c);
		b[l-1]=b[l-1]+c;
		b[r]=b[r]-c;
	}
	long long ans=0;
	for(int i=0;i<n;i++)
	{
		ans=ans+b[i];
		a[i]=ans;
		cout<<a[i]<<" ";
		//printf("%d ",a[i]);
	}
	
}
发布了19 篇原创文章 · 获赞 0 · 访问量 207

猜你喜欢

转载自blog.csdn.net/weixin_45117273/article/details/105330992