L - 登山道の満足度 / Satisfaction of the Mountain Trail Editorial by admin Editorial for printing
Claude 4.6 Opus (Thinking)Overview
This problem asks us to count the number of pairs \((l, r)\) representing a contiguous interval \([l, r]\) chosen from \(N\) sections, such that the sum of scenery values \(\sum S_i\) plus the minimum quietness value \(\min B_i\) (the satisfaction) is at least \(K\). We solve this efficiently using divide and conquer.
Analysis
Naive Approach
Enumerating all pairs \((l, r)\) yields \(O(N^2)\) combinations, which is too slow for \(N \leq 2 \times 10^5\).
Key Insight
The satisfaction formula is \(\text{sum}(S[l..r]) + \min(B[l..r])\). This expression mixes two different operations—”range sum” and “range minimum”—making it difficult to handle directly with just the two-pointer technique or segment trees alone.
However, using divide and conquer, we can restrict our attention to intervals that cross the midpoint \(\text{mid}\). In this case: - The left-side prefix sums and prefix minimums have monotonic properties - The same applies to the right side
By exploiting this monotonicity, we can efficiently count valid pairs using a combination of binary search and the two-pointer technique.
Algorithm
Divide and Conquer Framework
We divide the interval \([\text{lo}, \text{hi}]\) at the midpoint \(\text{mid}\) and count the following three cases: 1. Pairs contained entirely in the left half → recursion 2. Pairs contained entirely in the right half → recursion 3. Pairs crossing the midpoint (\(l \leq \text{mid} < r\)) → direct computation
Computing Pairs Crossing the Midpoint
We define index \(\text{li}\) extending left from \(\text{mid}\) (\(l = \text{mid} - \text{li}\)) and index \(\text{ri}\) extending right (\(r = \text{mid} + 1 + \text{ri}\)).
- \(\text{leftSum}[\text{li}]\): sum of \(S\) values for \(\text{li}+1\) elements to the left of \(\text{mid}\) (monotonically increasing)
- \(\text{leftMinB}[\text{li}]\): minimum of \(B\) values in the same range (monotonically non-increasing)
- \(\text{rightSum}[\text{ri}]\), \(\text{rightMinB}[\text{ri}]\): analogous for the right side
The satisfaction is \(\text{leftSum}[\text{li}] + \text{rightSum}[\text{ri}] + \min(\text{leftMinB}[\text{li}], \text{rightMinB}[\text{ri}])\).
Case Split on the Minimum
Case 1: \(\text{leftMinB}[\text{li}] \leq \text{rightMinB}[\text{ri}]\) (minimum comes from the left side)
The conditions are \(\text{rightSum}[\text{ri}] \geq K - \text{leftSum}[\text{li}] - \text{leftMinB}[\text{li}]\) and \(\text{rightMinB}[\text{ri}] \geq \text{leftMinB}[\text{li}]\).
- Iterate \(\text{li}\) from \(\text{leftLen}-1\) down to \(0\) (\(\text{leftMinB}[\text{li}]\) is non-decreasing in this direction)
- Use the two-pointer technique to maintain the range \([0, \text{ptr}]\) of \(\text{ri}\) satisfying \(\text{rightMinB}[\text{ri}] \geq \text{leftMinB}[\text{li}]\)
- Since \(\text{rightSum}\) is monotonically increasing, use binary search to count the number of values meeting the threshold
Case 2: \(\text{leftMinB}[\text{li}] > \text{rightMinB}[\text{ri}]\) (minimum comes from the right side)
Symmetric to Case 1: iterate over \(\text{ri}\) while binary searching on the left side.
Complexity
- Time complexity: \(O(N \log^2 N)\) (binary search taking \(O(N \log N)\) at each level of divide and conquer, with \(O(\log N)\) levels)
- Space complexity: \(O(N)\) (total array size created at each level is \(O(N)\), recursion depth is \(O(\log N)\))
Implementation Notes
Since \(S_i \geq 1\), \(\text{leftSum}\) and \(\text{rightSum}\) are strictly monotonically increasing, allowing
lower_boundbinary search to work correctly.By splitting into two cases, we count all crossing pairs without duplication (Case 1 uses \(\leq\), Case 2 uses strictly \(>\)).
Since values can reach up to \(2 \times 10^{14}\), it is necessary to use
long long.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
long long K;
cin >> N >> K;
vector<long long> S(N), B(N);
for(int i = 0; i < N; i++) cin >> S[i] >> B[i];
long long ans = 0;
// Divide and conquer
function<void(int, int)> solve = [&](int lo, int hi) {
if(lo > hi) return;
if(lo == hi){
if(S[lo] + B[lo] >= K) ans++;
return;
}
int mid = (lo + hi) / 2;
solve(lo, mid);
solve(mid+1, hi);
// Count crossing pairs: l in [lo, mid], r in [mid+1, hi]
int leftLen = mid - lo + 1;
int rightLen = hi - mid;
// Build left arrays (index 0 = mid, index i = mid-i)
vector<long long> leftSum(leftLen), leftMinB(leftLen);
leftSum[0] = S[mid];
leftMinB[0] = B[mid];
for(int i = 1; i < leftLen; i++){
leftSum[i] = leftSum[i-1] + S[mid-i];
leftMinB[i] = min(leftMinB[i-1], B[mid-i]);
}
// Build right arrays (index 0 = mid+1, index i = mid+1+i)
vector<long long> rightSum(rightLen), rightMinB(rightLen);
rightSum[0] = S[mid+1];
rightMinB[0] = B[mid+1];
for(int i = 1; i < rightLen; i++){
rightSum[i] = rightSum[i-1] + S[mid+1+i];
rightMinB[i] = min(rightMinB[i-1], B[mid+1+i]);
}
// Case 1: leftMinB[li] <= rightMinB[ri]
// Iterate li from 0 to leftLen-1 (leftMinB non-increasing)
// For each li, find max ri such that rightMinB[ri] >= leftMinB[li]
// Among those ri in [0, ptr], count rightSum[ri] >= K - leftSum[li] - leftMinB[li]
{
int ptr = rightLen - 1; // rightMinB is non-increasing
for(int li = leftLen - 1; li >= 0; li--){
// leftMinB[li] is non-increasing as li increases
// We need rightMinB[ri] >= leftMinB[li]
while(ptr >= 0 && rightMinB[ptr] < leftMinB[li]) ptr--;
if(ptr < 0) continue;
// valid ri in [0, ptr], rightSum is increasing
long long threshold = K - leftSum[li] - leftMinB[li];
// count ri in [0, ptr] with rightSum[ri] >= threshold
int pos = (int)(lower_bound(rightSum.begin(), rightSum.begin() + ptr + 1, threshold) - rightSum.begin());
ans += (ptr + 1 - pos);
}
}
// Case 2: leftMinB[li] > rightMinB[ri]
// Iterate ri from 0 to rightLen-1 (rightMinB non-increasing)
// For each ri, find max li such that leftMinB[li] > rightMinB[ri]
// Among those li in [0, ptr], count leftSum[li] >= K - rightSum[ri] - rightMinB[ri]
{
int ptr = leftLen - 1;
for(int ri = rightLen - 1; ri >= 0; ri--){
while(ptr >= 0 && leftMinB[ptr] <= rightMinB[ri]) ptr--;
if(ptr < 0) continue;
long long threshold = K - rightSum[ri] - rightMinB[ri];
int pos = (int)(lower_bound(leftSum.begin(), leftSum.begin() + ptr + 1, threshold) - leftSum.begin());
ans += (ptr + 1 - pos);
}
}
};
solve(0, N-1);
cout << ans << endl;
return 0;
}
This editorial was generated by claude4.6opus-thinking.
posted:
last update: