D - 花畑の散歩道 / A Walk Through the Flower Garden Editorial by admin Editorial for printing
gemini-3.5-flash-thinkingOverview
This problem asks us to maximize the total beauty of flowers we can obtain when traveling along a number line starting from home (coordinate \(0\)) with a total travel distance of at most \(D\).
By fixing the “leftmost point” and “rightmost point” of the travel path, we can organize the distance constraints and flower acquisition conditions, and solve the problem efficiently in \(O(M \log M)\) using plane sweep (2D queries) and a Binary Indexed Tree (BIT).
Analysis
1. Formulating the Walk Range and Travel Distance
Let \(-A\) (\(A \ge 0\)) be the leftmost coordinate reached during the walk, and \(B\) (\(B \ge 0\)) be the rightmost coordinate reached. The range covered during the walk is the interval \([-A, B]\).
The minimum travel distance to reach both the left end \(-A\) and the right end \(B\) starting from home (coordinate \(0\)) is the shorter of the following two patterns: - Going left first, then right: Moving \(0 \to -A \to B\), the distance is \(A + (A + B) = 2A + B\) - Going right first, then left: Moving \(0 \to B \to -A\), the distance is \(B + (B + A) = A + 2B\)
Therefore, the minimum travel distance is \(\min(2A + B, A + 2B)\). Since this must be at most \(D\), the condition to satisfy is: $\(\min(2A + B, A + 2B) \le D\)$
2. Conditions for Completing a Bouquet
To complete a bouquet of type \(j\), we need to reach both \(L_j\) and \(R_j\) during the walk. When the walk’s reachable range is \([-A, B]\), if both \(L_j\) and \(R_j\) are contained within this range, we will necessarily pass through both points during the movement. Therefore, the condition for obtaining flower type \(j\) is: $\(-A \le L_j \quad \text{and} \quad R_j \le B\)$
3. Narrowing Down Candidates for \(A\) and \(B\)
While \(A\) and \(B\) could take any real values, there is no need to move unnecessarily far. The boundaries for obtaining flowers are determined by the endpoints of each flower. Therefore, the optimal candidates for \(A\) and \(B\) can be limited as follows: - Candidates for \(A\): \(0\) and \(-L_j\) (for those with \(L_j \le 0\)) - Candidates for \(B\): \(0\) and \(R_j\) (for those with \(R_j \ge 0\))
Each of these has at most \(M + 1\) candidates.
4. Creating Queries
For each candidate of \(A\), the maximum \(B\) such that the travel distance is at most \(D\) can be found by solving the inequality \(\min(2A + B, A + 2B) \le D\): $\(B = \max\left(D - 2A, \frac{D - A}{2}\right)\)$
Similarly, for each candidate of \(B\), the maximum \(A\) is: $\(A = \max\left(D - 2B, \frac{D - B}{2}\right)\)$
The \((A, B)\) pairs obtained this way (at most \(2M + 2\)) form the “queries to investigate.”
5. Optimization: 2D Queries and Plane Sweep
For each query \((A, B)\), we want to efficiently compute the sum of beauties \(V_j\) for all flowers \(j\) satisfying \(-L_j \le A\) and \(R_j \le B\). This can be processed efficiently using plane sweep with the following procedure:
- Sort queries \((A, B)\) in ascending order of \(A\).
- Sort flowers \(j\) in ascending order of \(-L_j\).
- Process \(A\) values in increasing order, sequentially inserting flowers \(j\) satisfying \(-L_j \le A\) into the data structure (BIT). Each flower is inserted with \(R_j\) as the key (position) and beauty \(V_j\) as the value.
- For query \((A, B)\), retrieve from the data structure “the sum of values at positions \(\le B\).”
Algorithm
- Extracting candidate points and removing duplicates:
Collect candidate points for \(A\) and \(B\), then sort and deduplicate them (
std::unique). - Generating queries: From each candidate point, create the maximum pair \((A, B)\) satisfying the conditions, and add it to the query list.
- Coordinate compression: To use as BIT indices, coordinate-compress all flower right endpoints \(R_j\) and query values of \(B\) together.
- Sorting:
- Sort queries in ascending order of \(A\).
- Sort flowers in ascending order of \(-L_j\) (descending order of \(L_j\)).
- Plane sweep (two pointers + BIT):
- Process queries in order.
- For the current query’s \(A\), add all flowers satisfying \(-L_j \le A\) to the BIT (insertion position is the coordinate-compressed \(R_j\)).
- Retrieve from the BIT the sum of values at positions \(\le\) the coordinate-compressed \(B\) (
query(get_idx(B))), and update the maximum.
Complexity
- Time complexity: \(O(M \log M)\)
- Sorting candidate points, coordinate compression, and sorting queries and flowers take \(O(M \log M)\).
- In the plane sweep, each flower is added to the BIT at most once, and each query performs one BIT query. Since BIT operations are \(O(\log M)\), this part is \(O(M \log M)\) overall.
- Space complexity: \(O(M)\)
- The memory required for flower information, queries, coordinate compression arrays, and the BIT is all \(O(M)\).
Implementation Notes
Coordinate compression: Since values of \(R_j\) and \(B\) can be up to \(10^9\) (or larger), they cannot be directly used as array indices. By sorting all occurring values, removing duplicates, and using their “rank (1st, 2nd, …)” as indices, we can manage them with a BIT of size \(O(M)\).
Integer division: In calculations like \(B = (D - A) / 2\), care must be taken to ensure C++ integer division (truncation toward zero) works correctly. In this case, since \(D, A, B \ge 0\), a simple
/ 2is sufficient.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Flower {
long long L, R, V;
};
struct BIT {
int n;
vector<long long> tree;
BIT(int n) : n(n), tree(n + 1, 0) {}
void add(int i, long long v) {
for (; i <= n; i += i & -i) tree[i] += v;
}
long long query(int i) {
long long s = 0;
for (; i > 0; i -= i & -i) s += tree[i];
return s;
}
};
int main() {
// Optimize standard I/O operations for performance
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int M;
long long D;
if (!(cin >> M >> D)) return 0;
vector<Flower> flowers(M);
vector<long long> A_cand, B_cand;
A_cand.push_back(0);
B_cand.push_back(0);
for (int i = 0; i < M; ++i) {
cin >> flowers[i].L >> flowers[i].R >> flowers[i].V;
if (flowers[i].L <= 0) {
A_cand.push_back(-flowers[i].L);
}
if (flowers[i].R >= 0) {
B_cand.push_back(flowers[i].R);
}
}
// Remove duplicates from candidates
sort(A_cand.begin(), A_cand.end());
A_cand.erase(unique(A_cand.begin(), A_cand.end()), A_cand.end());
sort(B_cand.begin(), B_cand.end());
B_cand.erase(unique(B_cand.begin(), B_cand.end()), B_cand.end());
// Generate candidate pairs of (A, B)
vector<pair<long long, long long>> queries;
for (long long A : A_cand) {
if (A <= D) {
long long B = max(D - 2 * A, (D - A) / 2);
queries.push_back({A, B});
}
}
for (long long B : B_cand) {
if (B <= D) {
long long A = max(D - 2 * B, (D - B) / 2);
queries.push_back({A, B});
}
}
// Sort queries by A ascending
sort(queries.begin(), queries.end());
// Coordinate compression for BIT indexing
vector<long long> coords;
for (const auto& f : flowers) {
coords.push_back(f.R);
}
for (const auto& q : queries) {
coords.push_back(q.second);
}
sort(coords.begin(), coords.end());
coords.erase(unique(coords.begin(), coords.end()), coords.end());
auto get_idx = [&](long long x) {
return lower_bound(coords.begin(), coords.end(), x) - coords.begin() + 1;
};
// Sort flowers by -L ascending (which means L descending)
sort(flowers.begin(), flowers.end(), [](const Flower& a, const Flower& b) {
return -a.L < -b.L;
});
BIT bit(coords.size());
long long max_ans = 0;
int idx = 0;
// Two-pointer sweep with BIT
for (const auto& q : queries) {
long long A = q.first;
long long B = q.second;
// Add flowers that satisfy L_j >= -A
while (idx < M && -flowers[idx].L <= A) {
bit.add(get_idx(flowers[idx].R), flowers[idx].V);
idx++;
}
// Query the sum of beauties of flowers with R_j <= B
max_ans = max(max_ans, bit.query(get_idx(B)));
}
cout << max_ans << "\n";
return 0;
}
This editorial was generated by gemini-3.5-flash-thinking.
posted:
last update: