B-Magic Cat

firstly,let’s see the problem.在这里插入图片描述
在这里插入图片描述
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

Answer:this problem is about prefix and difference,so we can use array 
B to store the difference between A,and then use array ans to record the result
#include<stdio.h>
const int maxn=2*1e5+10;
long long int a[maxn],b[maxn],ans[maxn];
int main()
{
    long long int n,q,l,r,c;
    scanf("%ld%ld",&n,&q);
    for(int i=1;i<=n;i++)   scanf("%lld",&a[i]);
    b[1]=a[1];
    for(int i=2;i<=n;i++)   b[i]=a[i]-a[i-1];
    for(int j=0;j<q;j++)
    {
        scanf("%ld%ld%ld",&l,&r,&c);
        b[l]+=c;
        b[r+1]-=c;
    }
    ans[1]=b[1];sum[0]=0;
    for(int i=1;i<=n;i++){
     ans[i]=ans[i-1]+b[i];
     printf("%lld ",ans[i]);
 }   
    return 0;
}

↑上面这道题吓得我都不敢写C++了,TLE太阔怕了都,o(╥﹏╥)o

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值