※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 14419번 문제인 Foehn Phenomena이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/14419
14419번: Foehn Phenomena
Initially, the altitudes of the Spot 0, 1, 2, 3 are 0, 4, 1, 8, respectively. After the tectonic movement on the first day, the altitudes become 0, 6, 3, 8, respectively. At that moment, the temperatures of the wind are 0, -6, 0, -5, respectively.
www.acmicpc.net
구간 땅이 융기하면, 땅이 융기하는 구간의 경계에서의 바람의 온도 변화값만 변하고 나머지 위치에서의 바람의 온도 변화값은 변하지 않는다는 점을 눈치챈다면, 매 쿼리마다 해당 경계의 값만을 계산하는 것으로 문제를 해결할 수 있다.
기존 높이차이때문에 발생하는 온도 차이를 없는 것으로 하고 높이 변화량을 적용한 뒤 새로운 높이차이때문에 발생하는 온도 차이를 다시 계산하는 것으로 최종 온도변화를 쉽게 계산할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
using namespace std;
typedef long long ll;
ll old;
ll diff[200001];
ll ans = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, Q; ll S, T; cin >> N >> Q >> S >> T;
cin >> old;
for (int i = 1; i <= N; i++) {
ll x = old, y; cin >> y;
if (x < y) ans -= (y - x) * S;
else ans += (x - y) * T;
diff[i] = y - x;
old = y;
}
while (Q--) {
int L, R; ll d; cin >> L >> R >> d;
if (L > 0) {
if (diff[L] > 0) ans += diff[L] * S;
else ans += diff[L] * T;
diff[L] += d;
if (diff[L] > 0) ans -= diff[L] * S;
else ans -= diff[L] * T;
}
if (R < N) {
R++;
if (diff[R] > 0) ans += diff[R] * S;
else ans += diff[R] * T;
diff[R] -= d;
if (diff[R] > 0) ans -= diff[R] * S;
else ans -= diff[R] * T;
}
cout << ans << '\n';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 3109 // C++] 빵집 (0) | 2021.09.15 |
---|---|
[BOJ 19598 // C++] 최소 회의실 개수 (0) | 2021.09.14 |
[BOJ 8986 // C++] 전봇대 (0) | 2021.09.12 |
[BOJ 15809 // C++] 전국시대 (0) | 2021.09.11 |
[BOJ 18324 // C++] Race (0) | 2021.09.10 |