AcWing 797 差分 https://www.acwing.com/problem/content/799/
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int N = 1e5 + 5; int a[N], b[N]; void insert(int L, int R, int c) { b[L] += c; b[R + 1] -= c; } int main() { int n, m; cin >> n >> m; for(int i = 1; i <= n; ++ i) { cin >> a[i]; insert(i, i, a[i]); } while(m --) { int L, R, c; cin >> L >> R >> c; insert(L, R, c); } for(int i = 1; i <= n; ++ i) { b[i] += b[i-1]; cout << b[i] << " "; } }
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int N = 1e5 + 5; int a[N], b[N]; void insert(int L, int R, int c) { b[L] += c; b[R + 1] -= c; } int main() { int n, m; cin >> n >> m; for(int i = 1; i <= n; ++ i) { cin >> a[i]; insert(i, i, a[i]); } while(m --) { int L, R, c; cin >> L >> R >> c; insert(L, R, c); } for(int i = 1; i <= n; ++ i) { b[i] += b[i-1]; cout << b[i] << " "; } }