题目描述
blablabla
样例
输入样例
6 3
1 2 2 1 2 1
1 3 1
3 5 1
1 6 1
```
输出样例
3 4 5 3 4 2
Java 代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static int N = 100010; // 数据规模为 10w
static int[] b = new int[N]; // b数组为 arr数组的差分
static int[] sum = new int[N]; // arr数组为 b数组的前缀和
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
String[] str = reader.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
String[] temp = reader.readLine().split(" ");
for (int i = 1; i <= n; i++) { //注意下标
sum[i] = Integer.parseInt(temp[i - 1]);
}
for (int i = 1; i <= n; i++) {
insert(i, i, sum[i]);
}
while (m-- > 0) {
String[] nums = reader.readLine().split(" ");
int l = Integer.parseInt(nums[0]);
int r = Integer.parseInt(nums[1]);
int value = Integer.parseInt(nums[2]);
insert(l, r, value);
}
//求b的前缀和
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + b[i];
log.write(sum[i] + " ");
}
//释放资源
reader.close();
log.flush();
log.close();
}
private static void insert(int l, int r, int value) {
b[l] += value;
b[r + 1] -= value;
}