PS
BOJ 19951 : 태상이의 훈련소 생활
lickelon
2024. 8. 9. 22:26
- 문제 링크 : boj.kr/19951
- 난이도 : G5
- 태그 : 누적합
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> arr(n);
for(auto &u: arr) cin >> u;
vector<int> amount(n+1);
for(int i = 0; i < m; i++) {
int a, b, k;
cin >> a >> b >> k;
amount[a-1] += k;
amount[b] -= k;
}
for(int i = 1; i <= n; i++) {
amount[i] += amount[i-1];
}
for(int i = 0; i < n; i++) {
cout << arr[i] + amount[i] << " ";
}
return 0;
}
풀이
지점별 변한 양을 누적합을 통해 구한다. 원래의 높이와 더한 후 출력한다.
728x90