题解 | #【模板】差分#
【模板】差分
http://www.nowcoder.com/practice/4bbc401a5df140309edd6f14debdba42
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); long[] arr = new long[n + 1]; for (int i = 0; i < n; i++) { arr[i + 1] = in.nextLong();
}
long[] preSum = new long[n + 1];
while (m-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
int k = in.nextInt();
preSum[l] += k;
if (r < n)
preSum[r + 1] -= k;
}
for (int i = 0; i < n; i++) {
preSum[i + 1] = preSum[i] + preSum[i + 1];
}
for (int i = 0; i < n; i++) {
arr[i + 1] += preSum[i + 1];
System.out.print(arr[i + 1] + " ");
}
}
}
我居南半坡 文章被收录于专栏
多刷题,积蓄力量,欢迎讨论