Official

L - 登山道の満足度 / Satisfaction of the Mountain Trail Editorial by admin

gpt-5.3-codex

Overview

This problem asks us to count the number of intervals \([l,r]\) whose satisfaction value \(\sum_{j=l}^r S_j + \min(B_l,\dots,B_r)\) is at least \(K\).
We decompose the problem by which position is responsible for the interval minimum (\(\min B\)), then efficiently count using prefix sums + binary search.

Analysis

First, let’s reorganize the formula. For an interval \([l,r]\):

[ \sum_{j=l}^r S_j = \mathrm{pref}[r]-\mathrm{pref}[l-1] ]

So the condition becomes:

[ \mathrm{pref}[r]-\mathrm{pref}[l-1] + \min(B_l,\dots,B_r)\ge K ]


Naively checking all \(O(N^2)\) intervals and computing the minimum for each is far too slow (\(N\le 2\times 10^5\)).
The key insight is as follows.

1) Uniquely assign each interval based on which index is responsible for the interval minimum

For each \(i\), we collectively count “the intervals where \(B_i\) is the minimum.”
To avoid double-counting when there are ties, we use a monotonic stack to compute the following:

  • \(L[i]\): the position just after the nearest index to the left where \(B\) is strictly smaller (\(<\))
  • \(R[i]\): the position just before the nearest index to the right where \(B\) is smaller or equal (\(\le\))

In code, this is achieved by popping with >= for the left computation and popping with > for the right computation to handle tie-breaking.

Then, the intervals assigned to \(i\) are exactly:

[ l\in [L[i],\,i],\quad r\in [i,\,R[i]] ]

and every interval is assigned to exactly one \(i\) without overlap.


2) Count valid pairs for a fixed responsible index \(i\)

Since \(\min B = B_i\), the condition becomes:

[ \mathrm{pref}[r]-\mathrm{pref}[l-1] \ge K-B_i \; (=:T) ]

Since \(S_i\ge1\), the prefix sum \(\mathrm{pref}\) is strictly increasing. Therefore, binary search can be applied.

  • Fixing \(l\), we can binary search for the minimum \(r\) satisfying the condition.
  • Conversely, fixing \(r\), we can binary search for the maximum \(l\) satisfying the condition.

Iterating over the larger side of the assigned range would be slow, so we compare leftCnt = i-L[i]+1 and rightCnt = R[i]-i+1, loop over the smaller side, and binary search on the larger side for efficiency.

Algorithm

  1. Read input and build the prefix sum \(\mathrm{pref}\).
  2. Use a monotonic stack to compute \(L[i]\) and \(R[i]\) for each \(i\).
    • From the left: pop while B[st.back()] >= B[i]
    • From the right: pop while B[st.back()] > B[i]
  3. For each \(i\):
    • Compute \(T=K-B_i\)
    • The candidate intervals are \(l\in[L[i],i], r\in[i,R[i]]\)
    • Compare the number of left candidates and right candidates, and iterate over the smaller side
    • Binary search on the other side to count the number of valid pairs and add to the answer
  4. Output the total.

Complexity

  • Time complexity: \(O(N\log N)\)
    (For each \(i\), iterate over the smaller side with binary search; this totals to this order overall)
  • Space complexity: \(O(N)\)
    (Arrays S, B, pref, L, R and the stack)

Implementation Notes

  • Prefix sums and the answer must use long long (values can be very large).

  • The inequality directions in the monotonic stack (left >=, right >) are extremely important. Reversing them causes duplicates or missed intervals.

  • Indices are implemented as 1-indexed, making pref[l-1] easy to handle.

  • Carefully handling binary search boundaries (e.g., pos = R[i]+1) with sentinels helps prevent bugs.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    long long K;
    cin >> N >> K;

    vector<long long> S(N + 1), B(N + 1);
    for (int i = 1; i <= N; i++) cin >> S[i] >> B[i];

    vector<long long> pref(N + 1, 0);
    for (int i = 1; i <= N; i++) pref[i] = pref[i - 1] + S[i];

    vector<int> L(N + 1), R(N + 1);
    {
        vector<int> st;
        st.reserve(N);
        for (int i = 1; i <= N; i++) {
            while (!st.empty() && B[st.back()] >= B[i]) st.pop_back();
            L[i] = st.empty() ? 1 : st.back() + 1;
            st.push_back(i);
        }
    }
    {
        vector<int> st;
        st.reserve(N);
        for (int i = N; i >= 1; i--) {
            while (!st.empty() && B[st.back()] > B[i]) st.pop_back();
            R[i] = st.empty() ? N : st.back() - 1;
            st.push_back(i);
        }
    }

    auto count_leq = [&](int a, int b, int c, int d, long long M) -> long long {
        if (a > b || c > d) return 0LL;
        long long cnt = 0;
        int j = c;
        for (int l = a; l <= b; l++) {
            if (j < l) j = l;
            long long target = pref[l - 1] + M;
            while (j <= d && pref[j] <= target) j++;
            int hi = min(j - 1, d);
            if (hi >= c) {
                int lo = max(c, l);
                if (hi >= lo) cnt += (long long)(hi - lo + 1);
            }
        }
        return cnt;
    };

    long long ans = 0;

    for (int i = 1; i <= N; i++) {
        long long T = K - B[i];

        int leftCnt = i - L[i] + 1;
        int rightCnt = R[i] - i + 1;

        if (leftCnt <= rightCnt) {
            for (int l = L[i]; l <= i; l++) {
                long long need = pref[l - 1] + T;
                int lo = i, hi = R[i], pos = R[i] + 1;
                while (lo <= hi) {
                    int mid = (lo + hi) >> 1;
                    if (pref[mid] >= need) {
                        pos = mid;
                        hi = mid - 1;
                    } else {
                        lo = mid + 1;
                    }
                }
                if (pos <= R[i]) ans += (long long)(R[i] - pos + 1);
            }
        } else {
            for (int r = i; r <= R[i]; r++) {
                long long lim = pref[r] - T;
                int lo = L[i], hi = i, pos = L[i] - 1;
                while (lo <= hi) {
                    int mid = (lo + hi) >> 1;
                    if (pref[mid - 1] <= lim) {
                        pos = mid;
                        lo = mid + 1;
                    } else {
                        hi = mid - 1;
                    }
                }
                if (pos >= L[i]) ans += (long long)(pos - L[i] + 1);
            }
        }
    }

    cout << ans << '\n';
    return 0;
}

This editorial was generated by gpt-5.3-codex.