[20250629] BOJ / P3 / 시그마 시그마 시그마 시그마 / 권혁준 #397
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/27471
🧭 풀이 시간
35분
👀 체감 난이도
✏️ 문제 설명
길이 N인 수열 A에 대해, 다음 값을 998,244,353으로 나눈 나머지를 구해보자.

🔍 풀이 방법
조합론적으로 접근해보면, 정답을 구할 때 각각의 원소가 몇 번 더해질까를 생각해볼 수 있다.
수열의 i번째 수 A[i]가 몇 번 더해지는지 구해보자.
당연히, A[i]보다 작거나 같은 수를 선택해야 A[i]가 정답에 더해질 수 있다.
A[i]의 왼쪽에 있으면서 더 작은 수들 A[j]에 대해, max(A[i], A[j])를 구하게 되는 경우의 수가 j * (N-i+1) 번 존재한다.
A[i]의 오른쪽에 있으면서 더 작은 수들 A[k]에 대해, max(A[i], A[k])를 구하게 되는 경우의 수가 i * (N-k+1) 번 존재한다.
따라서, A[i]가 더해지는 횟수는 (j * (N-i+1)의 합) + (i * (N-k+1)의 합) 이고,
A[i]가 정답에 영향을 미치는 정도는 A[i] * ((j * (N-i+1)의 합) + (i * (N-k+1)의 합)) 이다.
저 합을 빠르게 구하기 위해서, 세그먼트 트리를 두 개 사용했다.
각각 왼쪽 끝에서 떨어진 정도와, 오른쪽 끝에서 떨어진 정도를 구간 합 세그먼트 트리로 두었다.
⏳ 회고
최근 풀었던 문제들 중에 가장 재밌었음