E - 花壇の区間選び / Choosing Flowerbed Intervals Editorial by admin Editorial for printing
gemini-3.5-flash-thinkingOverview
This problem asks you to count the number of contiguous intervals \([l, r]\) in a flower bed of length \(N\) that simultaneously satisfy the following two conditions:
- Condition 1 (Variety diversity): (Number of distinct varieties \(D\) in the interval) \(\times\) (length of the interval \(r - l + 1\)) \(\le K\)
- Condition 2 (Height balance): (Maximum height in the interval) \(-\) (Minimum height in the interval) \(\le M\)
Since the constraint is \(N \le 2 \times 10^5\), checking all intervals individually would take \(O(N^2)\) or more time, which exceeds the time limit. In this editorial, we focus on the “monotonicity” of intervals and explain how to solve the problem in \(O(N)\) by combining the Two Pointers technique with Sliding Maximum/Minimum (deque).
Analysis
Utilizing Monotonicity
Consider fixing the right endpoint \(r\) and moving the left endpoint \(l\) to the right to shrink the interval \([l, r]\).
Regarding Condition 1 When the interval shrinks, the interval length \((r - l + 1)\) always decreases. Also, the number of distinct varieties \(D\) either decreases or stays the same. Therefore, the value \(D \times (r - l + 1)\) monotonically decreases (or remains unchanged) as \(l\) increases. Thus, if \(l_1\) is the minimum left endpoint satisfying Condition 1 for a given \(r\), then Condition 1 is satisfied for any \(l \ge l_1\).
Regarding Condition 2 When the interval shrinks, the maximum value in the interval either decreases or stays the same, and the minimum value either increases or stays the same. Therefore, the difference between the maximum and minimum values monotonically decreases (or remains unchanged) as \(l\) increases. Thus, if \(l_2\) is the minimum left endpoint satisfying Condition 2 for a given \(r\), then Condition 2 is satisfied for any \(l \ge l_2\).
Counting the Valid Intervals
When the right endpoint \(r\) is fixed, the range of left endpoints \(l\) that satisfy both conditions simultaneously is: $\(\max(l_1, l_2) \le l \le r\)\( The number of such \)l\( values is \)r - \text{limit} + 1\( when \)\text{limit} = \max(l_1, l_2)\( and \)\text{limit} \le r$.
Furthermore, when advancing the right endpoint \(r\) to the right, the lower bounds \(l_1\) and \(l_2\) of the left endpoint required to satisfy the conditions only move to the right (monotonically non-decreasing). Using this property, we can manage the left pointers \(l_1\) and \(l_2\) in a two-pointer fashion while moving the right endpoint \(r\).
Algorithm
We advance the right endpoint \(r\) from \(0\) to \(N-1\) in order, performing the following operations:
1. Managing Condition 2 (Height balance): Pointer \(l_2\)
To efficiently obtain the maximum and minimum values within the interval, we use the Sliding Maximum/Minimum technique (using deque).
max_dq: A double-ended queue that stores indices of elements in the interval, maintaining values in descending order.min_dq: A double-ended queue that stores indices of elements in the interval, maintaining values in ascending order.
When adding a new element \(B[r]\), we remove elements from the back of the queue that are “inappropriate as maximum/minimum values” compared to \(B[r]\), then add \(r\).
After that, as long as B[max_dq[0]] - B[min_dq[0]] > M (the difference between the current maximum and minimum exceeds \(M\)), we advance the left endpoint \(l_2\) to the right. At this point, if the index at the front of the queue becomes smaller than \(l_2\), we remove it from the queue.
2. Managing Condition 1 (Variety diversity): Pointer \(l_1\)
We manage the occurrence count of each variety in the interval \([l_1, r]\) using an array cnt, and the number of distinct varieties using a variable \(D\).
- Add \(A[r]\), and if it is a newly appearing variety, increment \(D\) by \(1\).
- As long as \(D \times (r - l_1 + 1) > K\), advance the left endpoint \(l_1\) to the right. When advancing, decrement the occurrence count of the removed element \(A[l_1]\) by \(1\), and if the occurrence count becomes \(0\), decrement \(D\) by \(1\).
3. Accumulating the Answer
For each \(r\), the minimum left endpoint satisfying the conditions is \(\text{limit} = \max(l_1, l_2)\). If \(\text{limit} \le r\), add \(r - \text{limit} + 1\) intervals to the answer.
Complexity
Time Complexity: \(O(N)\)
- The right pointer \(r\) increases by \(1\) from \(0\) to \(N-1\).
- The left pointers \(l_1\) and \(l_2\) each advance to the right at most \(N\) times.
- Addition and removal of elements to/from the
dequeare performed at most once per element. - Therefore, all operations are performed in \(O(N)\) overall.
Space Complexity: \(O(N)\)
- We use an array
cnt(size \(N+1\)) to record the occurrence count of varieties, anddeques (maximum size \(N\)) to manage the maximum and minimum values, so the space complexity is \(O(N)\).
- We use an array
Implementation Notes
Index Management: The
dequestores “indices” rather than the values themselves. This allows us to determine in constant time whether the front element of the queue is outside the current interval (max_dq[0] < l2) when the left endpoint \(l_2\) advances.Overflow Caution: The expression \(D \times (r - l_1 + 1)\) in Condition 1 can reach approximately \(N^2 \approx 4 \times 10^{10}\) at maximum. In Python, this is automatically handled as arbitrary-precision integers, but when implementing in other languages such as C++, use a 64-bit integer type (such as
long long) to prevent overflow during computation.Source Code
import sys
from collections import deque
def solve():
input = sys.stdin.read
data = input().split()
if not data:
return
N = int(data[0])
K = int(data[1])
M = int(data[2])
A = [int(x) for x in data[3:3+N]]
B = [int(x) for x in data[3+N:3+2*N]]
# 条件2 (高さのバランス) を管理するための deque とポインタ
max_dq = deque()
min_dq = deque()
l2 = 0
# 条件1 (品種の多様さ) を管理するための頻度配列とポインタ
cnt = [0] * (N + 1)
D = 0
l1 = 0
ans = 0
for r in range(N):
# 条件2の更新 (B[r] を追加)
br = B[r]
while max_dq and B[max_dq[-1]] <= br:
max_dq.pop()
max_dq.append(r)
while min_dq and B[min_dq[-1]] >= br:
min_dq.pop()
min_dq.append(r)
# 高さの差が M 以下になるまで l2 を進める
while B[max_dq[0]] - B[min_dq[0]] > M:
l2 += 1
if max_dq[0] < l2:
max_dq.popleft()
if min_dq[0] < l2:
min_dq.popleft()
# 条件1の更新 (A[r] を追加)
ar = A[r]
if cnt[ar] == 0:
D += 1
cnt[ar] += 1
# D * (r - l1 + 1) <= K を満たすまで l1 を進める
while D * (r - l1 + 1) > K:
al1 = A[l1]
cnt[al1] -= 1
if cnt[al1] == 0:
D -= 1
l1 += 1
# 両方の条件を満たす左端の下限
limit = l1 if l1 > l2 else l2
if limit <= r:
ans += r - limit + 1
print(ans)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3.5-flash-thinking.
posted:
last update: