[20251023] BOJ / D4 / 수열과 쿼리 0 / 권혁준 #1207
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/13545
🧭 풀이 시간
30분
👀 체감 난이도
✏️ 문제 설명
1과 -1로만 이루어져 있는 길이 N인 수열 A에 아래 쿼리들을 처리해보자.
🔍 풀이 방법
어떤 부분 수열 A[i..j]가 합이 0이라는 것은, A의 누적 합 배열 s에서 s[j] - s[i-1] = 0이라는 것과 똑같다.
-> 쿼리에서 현재 구간을 관리할 때 어떤 원소 s[i]에 대해, 이 값을 가지는 인덱스들을 모두 덱 v[s[i]]에 저장해둔다.
일단 mo's로 쿼리를 정렬한다.
인덱스 i가 현재 구간에 추가된다면, 기존의 v[s[i]]에 i를 추가해서 해당 원소를 마지막으로 갖는 최대 길이를 높여준다.
인덱스 i가 현재 구간에서 빠지게 되면, 기존의 v[s[i]]에서 i를 빼서 해당 원소를 마지막으로 가지던 최대 길이를 낮춰준다.
최대 길이를 효율적으로 구하기 위해서 각 누적 합 원소에 대한 인덱스를 저장하는 덱을 썼고,
각 쿼리에 대한 답을 구할 때는 길이에 대한 카운트 배열 + 제곱근 분할 카운트 배열로 쿼리 당 O(sqrt(N))에 구해줬다.
⏳ 회고
덱이 비어있는지 확인을 안 해서
AccessEmptyContainer에러가 떴다 조심하자