L - 登山道の満足度 / Satisfaction of the Mountain Trail Editorial by admin Editorial for printing
gpt-5.5-xhighOverview
Given a contiguous interval \([l,r]\), the problem asks us to count the number of intervals where the sum of scenery values \(S_l+\cdots+S_r\) plus the minimum quietness \(\min(B_l,\ldots,B_r)\) is at least \(K\).
The key idea is to uniquely classify each interval by “the position responsible for the minimum quietness value,” and for each classification, count using prefix sums and binary search.
Analysis
Naively checking all intervals \([l,r]\) would require examining \(O(N^2)\) intervals.
Since \(N \le 2 \times 10^5\), this is too slow.
Additionally, the standard two-pointer technique is difficult to apply here.
This is because when extending the right endpoint, while the sum of scenery values increases, the minimum quietness can drop significantly, so the satisfaction does not necessarily increase monotonically.
For example, with \(S=[1,1]\), \(B=[100,1]\):
- The satisfaction of interval \([1,1]\) is \(1+100=101\)
- The satisfaction of interval \([1,2]\) is \(2+1=3\)
So extending the interval actually decreases satisfaction.
Therefore, we classify intervals by “which position’s \(B_i\) is adopted as the minimum value.”
To do this, we use a minimum-value Cartesian Tree on the sequence \(B\).
A Cartesian Tree is a binary tree with the following properties:
- An in-order traversal yields the original index order \(1,2,\ldots,N\)
- A parent’s \(B\) value is less than or equal to its children’s \(B\) values
- The subtree of each node corresponds to a contiguous interval in the original array
When there are duplicate \(B_i\) values, the code prioritizes the leftmost one.
This is because during Cartesian Tree construction, we only pop when B[st.back()] > B[i].
Let \([L_u,R_u]\) denote the interval represented by the subtree of node \(u\).
Then, an interval \([x,y]\) corresponds to node \(u\) if:
- \(L_u \le x \le u\)
- \(u \le y \le R_u\)
In other words, the interval contains \(u\) and lies within the range of \(u\)’s subtree.
With this classification, every interval corresponds to exactly one node.
For intervals corresponding to node \(u\), the minimum quietness is \(B_u\).
Therefore, we need to count pairs \(x,y\) satisfying:
\(S_x+\cdots+S_y+B_u \ge K\)
Using the prefix sum \(P_i=S_1+\cdots+S_i\), we have:
\(S_x+\cdots+S_y=P_y-P_{x-1}\)
So the condition becomes:
\(P_y-P_{x-1}+B_u \ge K\)
That is:
\(P_y-P_{x-1} \ge K-B_u\)
Algorithm
First, compute the prefix sum \(P\) of the scenery values \(S\).
Next, build the minimum-value Cartesian Tree on \(B\).
After construction, for each node \(u\), determine the interval \([L_u,R_u]\) represented by its subtree.
For node \(u\), the intervals we want to count satisfy:
- Left endpoint \(x\): \(L_u \le x \le u\)
- Right endpoint \(y\): \(u \le y \le R_u\)
Substituting \(t=x-1\):
- Range of \(t\): \(L_u-1 \le t \le u-1\)
- Range of \(y\): \(u \le y \le R_u\)
The condition is:
\(P_y-P_t \ge K-B_u\)
Let \(T=K-B_u\).
Case 1: \(T \le 0\)
In this case, since the sum of scenery values is positive, all candidate intervals satisfy the condition.
The number of candidates is:
\((u-L_u+1)(R_u-u+1)\)
Case 2: \(T > P_{R_u}-P_{L_u-1}\)
This is the case where even selecting the entire subtree doesn’t provide enough scenery sum.
Since all candidate intervals have length at most that of the entire subtree, none satisfy the condition.
Case 3: Count using binary search
For the remaining cases, we use binary search leveraging the fact that the prefix sum is monotonically increasing.
The number of candidates on the left side is:
\(u-L_u+1\)
The number of candidates on the right side is:
\(R_u-u+1\)
Iterating over both sides would be too slow, so we iterate over only the smaller side and use binary search on the larger side.
When the left side is smaller
Iterate over \(t=x-1\).
The condition is:
\(P_y \ge P_t+T\)
So among \(y \in [u,R_u]\), find the first position where \(P_y \ge P_t+T\) using lower_bound.
If that position is pos, then the right endpoints satisfying the condition are:
\(pos,pos+1,\ldots,R_u\)
So the count is:
\(R_u-pos+1\)
When the right side is smaller
Iterate over \(y\).
The condition is:
\(P_t \le P_y-T\)
So among \(t \in [L_u-1,u-1]\), count the number of \(t\) satisfying \(P_t \le P_y-T\) using upper_bound.
This allows us to count, for each node \(u\), the intervals corresponding to it that satisfy the condition.
Summing over all nodes gives the answer.
Complexity
- Time complexity: \(O(N\log^2 N)\)
- Space complexity: \(O(N)\)
Building the Cartesian Tree and computing the subtree ranges for each node takes \(O(N)\).
Since we only enumerate the smaller side for each node, the total number of enumerations is \(O(N\log N)\).
Since each enumeration involves a binary search, the overall complexity is \(O(N\log^2 N)\).
Implementation Notes
The answer can be as large as \(N(N+1)/2\), which for \(2 \times 10^5\) is approximately \(2 \times 10^{10}\), so
long longis required.The prefix sum can also reach approximately \(2 \times 10^{14}\), so
long longshould be used.The Cartesian Tree can degenerate into a straight-line tree in the worst case, so recursive DFS may cause a stack overflow. The code uses an explicit stack for traversal.
To prevent double-counting when there are duplicate \(B_i\) values, the comparison during Cartesian Tree construction must be consistent. The code uses
>, prioritizing the leftmost occurrence of equal values.Source Code
#include <bits/stdc++.h>
using namespace std;
static inline int lower_idx(const vector<long long>& v, int l, int r, long long x) {
while (l < r) {
int m = (l + r) >> 1;
if (v[m] < x) l = m + 1;
else r = m;
}
return l;
}
static inline int upper_idx(const vector<long long>& v, int l, int r, long long x) {
while (l < r) {
int m = (l + r) >> 1;
if (v[m] <= x) l = m + 1;
else r = m;
}
return l;
}
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), pref(N + 1, 0);
for (int i = 1; i <= N; i++) {
cin >> S[i] >> B[i];
pref[i] = pref[i - 1] + S[i];
}
vector<int> left_ch(N + 1, 0), right_ch(N + 1, 0), parent(N + 1, 0);
vector<int> st;
st.reserve(N);
for (int i = 1; i <= N; i++) {
int last = 0;
while (!st.empty() && B[st.back()] > B[i]) {
last = st.back();
st.pop_back();
}
if (!st.empty()) {
right_ch[st.back()] = i;
parent[i] = st.back();
}
if (last != 0) {
left_ch[i] = last;
parent[last] = i;
}
st.push_back(i);
}
int root = 1;
for (int i = 1; i <= N; i++) {
if (parent[i] == 0) {
root = i;
break;
}
}
vector<int> order;
order.reserve(N);
vector<int> stack_nodes;
stack_nodes.push_back(root);
while (!stack_nodes.empty()) {
int u = stack_nodes.back();
stack_nodes.pop_back();
order.push_back(u);
if (left_ch[u]) stack_nodes.push_back(left_ch[u]);
if (right_ch[u]) stack_nodes.push_back(right_ch[u]);
}
vector<int> L(N + 1), R(N + 1);
for (int i = (int)order.size() - 1; i >= 0; i--) {
int u = order[i];
L[u] = left_ch[u] ? L[left_ch[u]] : u;
R[u] = right_ch[u] ? R[right_ch[u]] : u;
}
long long ans = 0;
for (int u = 1; u <= N; u++) {
int l = L[u], r = R[u];
long long threshold = K - B[u];
long long a = u - l + 1LL;
long long b = r - u + 1LL;
if (threshold <= 0) {
ans += a * b;
continue;
}
if (threshold > pref[r] - pref[l - 1]) {
continue;
}
if (a <= b) {
for (int t = l - 1; t <= u - 1; t++) {
long long need = pref[t] + threshold;
int pos = lower_idx(pref, u, r + 1, need);
ans += r - pos + 1LL;
}
} else {
for (int end = u; end <= r; end++) {
long long limit = pref[end] - threshold;
int pos = upper_idx(pref, l - 1, u, limit);
ans += pos - (l - 1LL);
}
}
}
cout << ans << '\n';
return 0;
}
This editorial was generated by gpt-5.5-xhigh.
posted:
last update: