4448: 【基础】倒水(2062)

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:15 解决:6

题目描述

在一个桌子上摆放了 lns="http://www.w3.org/1998/Math/MathML"> 个杯子,每个杯子中有一定量的水。小 lns="http://www.w3.org/1998/Math/MathML"> 同学负责向杯子中倒水,他总共倒了 lns="http://www.w3.org/1998/Math/MathML"> 次,每次会向从第 lns="http://www.w3.org/1998/Math/MathML"> 个杯子到第 lns="http://www.w3.org/1998/Math/MathML"> 个杯子中添加 lns="http://www.w3.org/1998/Math/MathML"> 毫升的水(注意:水只可能增加,不可能减少)。

请问小 lns="http://www.w3.org/1998/Math/MathML"> 同学倒了 lns="http://www.w3.org/1998/Math/MathML"> 次水之后, lns="http://www.w3.org/1998/Math/MathML"> 个杯子每个杯子有多少毫升的水。

输入

第一行包含两个整数 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML">

第二行包含 lns="http://www.w3.org/1998/Math/MathML"> 个整数,表示一开始每个杯子中水的毫升数。

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行包含三个整数 lns="http://www.w3.org/1998/Math/MathML">,,,表示一次操作。

数据范围

lns="http://www.w3.org/1998/Math/MathML">1,100000

lns="http://www.w3.org/1998/Math/MathML">1,lns="http://www.w3.org/1998/Math/MathML">01000

杯子中水的初始量在 lns="http://www.w3.org/1998/Math/MathML">[0,1000] 的范围内。

本题数据上保证所有的杯子在加水之后,水量值仍然在 int 范围内

输出

共一行,包含 lns="http://www.w3.org/1998/Math/MathML"> 个整数,表示最终 lns="http://www.w3.org/1998/Math/MathML"> 个杯子每个杯子有多少毫升的水。

样例输入 复制

8 3
1 2 10 8 1 5 1 1
7 8 12
1 8 4
2 3 12

样例输出 复制

5 18 26 12 5 9 17 17

提示


//解法一:利用原数组求差分数组
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
int a[N],b[N]; //a代表读入的原数组,b代表是差分数组
int n,k,l,r,p;
int main(){
    cin>>n>>k;
    for(int i=1;i<= n;i++){
        cin>>a[i]; //求差分数组
        b[i]= a[i]- a[i-1];
    }
    //k 次操作
    for(int i= 1;i<= k;i++){
        cin>>l>>r>>p;
	    b[l]=b[l]+ p;
	    b[r+1]=b[r+1]-p;
    }
    //求b数组的前缀和,就是a数组做了k次操作的结果
    for(int i=1;i<= n;i++){
	    b[i]= b[i-1] + b[i];
        cout<<b[i]<<" ";
    }
    return 0;
}


//解法二:读入时直接求差分数组
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
int a[N],b[N]; //a代表读入的原数组,b代表是差分数组
int n,k,l,r,p;
int main(){
    cin>>n>>k;
    for(int i=1;i<= n;i++){
        //cin>>a[i]; //每读入一个数,直接求出对应的差分数组
        cin>>p;
        //求差分数组
        //b[i]= a[i]- a[i-1];
        b[i]= b[i]+ p;
        b[i+1]= b[i+1]- p;
    }
    //k 次操作
    for(int i= 1;i<= k;i++){
        cin>>l>>r>>p;
	    b[l]=b[l]+ p;
	    b[r+1]=b[r+1]-p;
	}
    //求b数组的前缀和,就是a数组做了k次操作的结果
    for(int i=1;i<= n;i++){
	    b[i]= b[i-1]+ b[i];
        cout<<b[i]<<" ";
    }
    return 0;
}