AcWing 797. 差分(Java)
原题链接
简单
作者:
火球大的脸盆
,
2022-07-07 19:44:41
,
所有人可见
,
阅读 108
//此题用Scanner过不了全部样例,必须优化输入输出。
import java.io.*;
public class Main {
public static int[] a = new int[100010], b = new int[100010];
public static int n, m, l, r, c;
public static void insert(int l, int r, int c) {
b[l] += c;
b[r + 1] -= c;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] first_line = br.readLine().split(" ");
String[] second_line = br.readLine().split(" ");
n = Integer.parseInt(first_line[0]);
m = Integer.parseInt(first_line[1]);
for (int i = 1; i <= n; i++) a[i] = Integer.parseInt(second_line[i - 1]);
for (int i = 1; i <= n; i++) insert(i, i, a[i]);
while (0 != m--) {
String[] querys = br.readLine().split(" ");
l = Integer.parseInt(querys[0]);
r = Integer.parseInt(querys[1]);
c = Integer.parseInt(querys[2]);
insert(l, r, c);
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 1; i <= n; i++) {
b[i] += b[i - 1];
bw.write(b[i] + " ");
bw.flush();
}
}
}