E - 上下に跳ぶ展示順 / Exhibition Order Jumping Up and Down Editorial by admin Editorial for printing
claude5.5opus-highOverview
We choose exhibits in increasing order of their indices such that the absolute difference in height between adjacent chosen exhibits lies within \([L,R]\), and the heights alternate between “increasing” and “decreasing”. Among all subsequences satisfying these conditions, find the maximum possible total score.
Analysis
State Representation
When building a subsequence from left to right, what can be appended next is determined solely by the following two factors:
- The height of the last chosen exhibit
- Whether the last step was an “increase” or a “decrease”
Therefore, for subsequences ending at exhibit \(i\), we consider the following two values:
- \(U_i\): The maximum score when reaching exhibit \(i\) where the last step is an increase (\(A_i >\) previous height)
- \(D_i\): The maximum score when reaching exhibit \(i\) where the last step is a decrease (\(A_i <\) previous height)
Since \(L\ge 1\), the height difference is never \(0\), so each step is strictly either an “increase” or a “decrease”.
Transitions
Consider the case where we reach \(i\) by an increase. The previous exhibit \(j\) (\(j<i\)) must satisfy the following two conditions: - Height condition: \(A_i - R \le A_j \le A_i - L\) - Alternation condition: Since the step after \(j\) is an increase, \(j\) must either “end with a decrease” or be “exhibit \(j\) alone (the first element)”.
Thus, $\(U_i = B_i + \max_{j<i,\ A_j\in[A_i-R,\,A_i-L]} \max(B_j, D_j)\)$
Similarly, when reaching \(i\) by a decrease: $\(D_i = B_i + \max_{j<i,\ A_j\in[A_i+L,\,A_i+R]} \max(B_j, U_j)\)$
The answer is the maximum among \(0\) (choosing nothing), \(B_i\) (choosing only one exhibit), \(U_i\), and \(D_i\) over all \(i\).
Issues with the Naive Approach
Checking all \(j<i\) for each \(i\) would take \(O(N^2)\) time, which results in TLE for \(N=200000\).
Optimization
What we need for the transitions is “the maximum value among \(j\) whose heights fall within a certain range”. This can be answered by finding the range maximum using a segment tree indexed by heights. By processing exhibits in increasing order of their indices and writing the values of processed exhibits into the segment tree, the condition “\(j<i\)” is naturally satisfied.
Since the heights can be up to \(10^9\), we coordinate-compress them before using them as segment tree indices.
We maintain two segment trees:
- segD: Stores \(\max(B_j, D_j)\) at height \(A_j\). This represents values that can be extended by a subsequent increase.
- segU: Stores \(\max(B_j, U_j)\) at height \(A_j\). This represents values that can be extended by a subsequent decrease.
If there are multiple exhibits with the same height, we keep the maximum value at that position.
Algorithm
- Coordinate-compress \(A\).
- Initialize two segment trees
segDandsegUwith \(-\infty\). Also set \(ans = 0\). - For each \(i = 1, \ldots, N\) in order, do the following:
- Find the range of compressed indices corresponding to heights in \([A_i-R,\ A_i-L]\) using binary search, and query the range maximum \(v\) from
segD. If a valid value exists, set \(U_i = B_i + v\). - Similarly, for the range corresponding to heights in \([A_i+L,\ A_i+R]\), compute \(D_i\) from the range maximum of
segU. - Update \(ans \leftarrow \max(ans, B_i, U_i, D_i)\).
- Update position \(A_i\) in
segDwith \(\max(B_i, D_i)\), and update position \(A_i\) insegUwith \(\max(B_i, U_i)\) (in both cases, only if the new value is greater than the existing value).
- Find the range of compressed indices corresponding to heights in \([A_i-R,\ A_i-L]\) using binary search, and query the range maximum \(v\) from
- Output \(ans\).
Example: When \(L=1, R=2\), and \((A,B) = (1,5), (3,-1), (2,4)\):
- Exhibit 1: Neither \(U\) nor \(D\) exists. segD[1] \(=\) segU[1] \(=5\).
- Exhibit 2 (height 3): The candidate heights for an increase prior to this are in the range \([1,2]\), which yields \(5\) from segD. Therefore, \(U_2 = -1 + 5 = 4\). We get segU[3] \(= \max(-1, 4) = 4\) and segD[3] \(= -1\).
- Exhibit 3 (height 2): The candidate heights for a decrease prior to this are in the range \([3,4]\), which yields \(4\) from segU. Therefore, \(D_3 = 4+4 = 8\).
This corresponds to \(1 \to 3 \to 2\) (increase, then decrease), and the answer is \(8\).
Complexity
- Time complexity: \(O(N \log N)\) (sorting for coordinate compression, binary search, and segment tree operations for each exhibit)
- Space complexity: \(O(N)\)
Implementation Points
Remember that a sequence can start with a single exhibit. By writing \(\max(B_i, D_i)\) or \(\max(B_i, U_i)\) into the segment trees, we naturally include the case where “\(i\) serves as the very first exhibit to connect to the next one”.
If there are no candidates within the range, the range maximum will be \(-\infty\). To avoid mistakenly treating \(-\infty + B_i\) as a valid score, check that
v > NEGbefore using it. Set \(-\infty\) to a value likeLLONG_MIN/4so that addition will not cause an overflow.The total score can be up to around \(2\times10^5 \times 10^9 = 2\times10^{14}\), so use
long long.To convert a height range \([lo, hi]\) into compressed indices, use
lower_bound(lo)andupper_bound(hi)to form a half-open interval \([l, r)\).Since scores can be negative, initialize the answer to \(0\) (choosing nothing).
Source Code
#include <bits/stdc++.h>
#include <atcoder/segtree>
using namespace std;
typedef long long ll;
const ll NEG = LLONG_MIN / 4;
ll op(ll a, ll b) { return max(a, b); }
ll e() { return NEG; }
int main() {
int n; ll L, R;
scanf("%d %lld %lld", &n, &L, &R);
vector<ll> A(n), B(n);
for (int i = 0; i < n; i++) scanf("%lld %lld", &A[i], &B[i]);
vector<ll> xs = A;
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
int m = xs.size();
atcoder::segtree<ll, op, e> segD(m), segU(m); // segD: values that can be followed by up; segU: followed by down
ll ans = 0;
auto rangeIdx = [&](ll lo, ll hi) {
int l = lower_bound(xs.begin(), xs.end(), lo) - xs.begin();
int r = upper_bound(xs.begin(), xs.end(), hi) - xs.begin();
return make_pair(l, r);
};
for (int i = 0; i < n; i++) {
ll U = NEG, D = NEG;
{
auto [l, r] = rangeIdx(A[i] - R, A[i] - L);
if (l < r) {
ll v = segD.prod(l, r);
if (v > NEG) U = B[i] + v;
}
}
{
auto [l, r] = rangeIdx(A[i] + L, A[i] + R);
if (l < r) {
ll v = segU.prod(l, r);
if (v > NEG) D = B[i] + v;
}
}
ans = max({ans, B[i], U, D});
int p = lower_bound(xs.begin(), xs.end(), A[i]) - xs.begin();
ll ed = max(B[i], D);
ll eu = max(B[i], U);
if (ed > segD.get(p)) segD.set(p, ed);
if (eu > segU.get(p)) segU.set(p, eu);
}
printf("%lld\n", ans);
}
This editorial was generated by claude5.5opus-high.
posted:
last update: