L - 登山道の満足度 / Satisfaction of the Mountain Trail Editorial by admin Editorial for printing
gpt-5.5-highOverview
This is a problem of counting contiguous intervals \([l,r]\) such that \(\sum S_i + \min B_i \ge K\).
We uniquely assign each interval to a “representative position that achieves \(\min B\)”, and efficiently count the number of intervals satisfying the condition for each representative position.
Analysis
The satisfaction of an interval \([l,r]\) can be written using the prefix sum \(P\) as:
\[ P_r - P_{l-1} + \min(B_l,\ldots,B_r) \]
However, checking all \((l,r)\) pairs would be \(O(N^2)\), which is too slow for \(N \le 2 \times 10^5\).
Moreover, when extending the right endpoint, \(\sum S_i\) increases but \(\min B_i\) may decrease, so the overall satisfaction is not necessarily monotone. Therefore, a simple two-pointer technique is also difficult to apply.
Instead, we classify each interval by “the position that achieves \(\min B\)”.
However, we need to avoid double-counting when the same minimum value appears at multiple positions.
In this solution, we choose the rightmost position achieving \(\min B\) within the interval as the representative.
For each \(i\), we define:
- \(p_i\): the nearest position to the left of \(i\) where \(B_j < B_i\)
If none exists, set it to \(0\) - \(q_i\): the nearest position to the right of \(i\) where \(B_j \le B_i\)
If none exists, set it to \(N+1\)
Then, the condition for position \(i\) to be the “rightmost minimum position” of interval \([l,r]\) is:
\[ p_i < l \le i \le r < q_i \]
The reasoning is as follows:
- If \(p_i < l\), there is no value smaller than \(B_i\) between \(l\) and \(i-1\)
- If \(r < q_i\), there is no value less than or equal to \(B_i\) between \(i+1\) and \(r\)
- Therefore, the minimum in the interval is \(B_i\), and if there are equal minimum values, they are only to the left of \(i\), so \(i\) is the rightmost minimum position
For example, if \(B=[5,2,3,2]\), the value \(B=2\) appears at positions \(2,4\).
The minimum of interval \([2,4]\) is \(2\), but the representative is the rightmost position \(4\).
By deciding this way, each interval is counted exactly once.
Algorithm
First, prepare the prefix sums:
\[ P_0=0,\quad P_i=S_1+\cdots+S_i \]
The sum of \(S\) over interval \([l,r]\) is:
\[ P_r-P_{l-1} \]
Next, compute \(p_i, q_i\) using a monotone stack.
How to compute \(p_i\)
Scan from left to right, maintaining a stack of indices where \(B\) values are strictly increasing.
For the current position \(i\), remove elements from the top of the stack while the \(B\) value at the top is greater than or equal to \(B_i\).
Then, the remaining top element is the “nearest \(B_j < B_i\) to the left”.
How to compute \(q_i\)
Scan from right to left, using a stack.
For the current position \(i\), remove elements from the top of the stack while the \(B\) value at the top is strictly greater than \(B_i\).
Then, the remaining top element is the “nearest \(B_j \le B_i\) to the right”.
The key point here is that we use \(<\) for the left side and \(\le\) for the right side.
This ensures that intervals with multiple occurrences of the same minimum are assigned only to the rightmost position.
Consider the intervals where position \(i\) is the representative.
The selectable range is:
\[ p_i < l \le i \le r < q_i \]
Substituting:
\[ x=l-1,\quad y=r \]
we get:
\[ x \in [p_i, i-1],\quad y \in [i, q_i-1] \]
The condition for satisfaction to be at least \(K\) is:
\[ P_y-P_x+B_i \ge K \]
that is:
\[ P_y-P_x \ge K-B_i \]
Let:
\[ need = K-B_i \]
Then we need to count:
the number of pairs with \(x \in [p_i, i-1]\), \(y \in [i, q_i-1]\) satisfying:
\[ P_y-P_x \ge need \]
Since all \(S_i\) are positive, the prefix sum \(P\) is strictly increasing.
Therefore, by fixing one side, we can count the other using binary search.
Fixing the left side
If we fix \(x\), we need to count the number of \(y\) satisfying:
\[ P_y \ge P_x + need \]
Since the prefix sum \(P\) is in ascending order, we can find the first position satisfying the condition using bisect_left.
Fixing the right side
If we fix \(y\), we need to count the number of \(x\) satisfying:
\[ P_x \le P_y - need \]
This can be counted using bisect_right.
Iterate over the smaller side
The number of choices for intervals with representative \(i\) is:
- Number of left choices: \(i-p_i\)
- Number of right choices: \(q_i-i\)
Iterating over the larger side each time would be slow, so in the code we iterate over the smaller side and binary search the other side.
Additionally, the following optimizations are applied:
- If \(need \le S_i\), the sum of \(S\) for any interval containing position \(i\) is at least \(S_i\), so all are valid
- If even the maximum \(P_{q_i-1}-P_{p_i} < need\), no interval satisfies the condition
Complexity
- Computing \(p_i, q_i\) with monotone stacks: \(O(N)\)
- For each \(i\), only the smaller side is traversed
The total number of times each position is traversed is bounded by \(O(N \log N)\).
This is because it has a Cartesian tree-like structure where intervals are split at minimum positions, and we always look at the smaller side only.
Since binary search is performed for each traversal, the overall time complexity is:
- Time complexity: \(O(N \log^2 N)\)
- Space complexity: \(O(N)\)
Implementation Notes
It is convenient to store the prefix sum \(P\) starting from \(0\), so that the interval sum can be written as \(P_r-P_{l-1}\).
\(p_i\) uses “previous \(B_j < B_i\)” and \(q_i\) uses “next \(B_j \le B_i\)”.
Reversing the inequalities or making both the same may lead to double-counting intervals with the same minimum value.The answer can be as large as approximately \(\frac{N(N+1)}{2}\), which may exceed \(2 \times 10^{10}\).
This is not a problem in Python, but in other languages, 64-bit integers must be used.Source Code
import sys
from bisect import bisect_left, bisect_right
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
N = data[0]
K = data[1]
B = [0] * (N + 2)
P = [0] * (N + 1)
idx = 2
acc = 0
for i in range(1, N + 1):
s = data[idx]
b = data[idx + 1]
idx += 2
acc += s
P[i] = acc
B[i] = b
prev_less = [0] * (N + 2)
next_le = [N + 1] * (N + 2)
st = []
for i in range(1, N + 1):
bi = B[i]
while st and B[st[-1]] >= bi:
st.pop()
if st:
prev_less[i] = st[-1]
st.append(i)
st.clear()
for i in range(N, 0, -1):
bi = B[i]
while st and B[st[-1]] > bi:
st.pop()
if st:
next_le[i] = st[-1]
st.append(i)
ans = 0
bl = bisect_left
br = bisect_right
for i in range(1, N + 1):
pi = prev_less[i]
ni = next_le[i]
left_len = i - pi
right_len = ni - i
need = K - B[i]
if need <= P[i] - P[i - 1]:
ans += left_len * right_len
continue
if P[ni - 1] - P[pi] < need:
continue
if left_len <= right_len:
hi = ni
for x in range(pi, i):
pos = bl(P, P[x] + need, i, hi)
ans += hi - pos
else:
lo = pi
for y in range(i, ni):
cnt = br(P, P[y] - need, lo, i) - lo
ans += cnt
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.
posted:
last update: