Skip to content

Commit 7ac1a95

Browse files
committed
Update content/5_Plat/Range_Sweep.mdx
1 parent 4eed262 commit 7ac1a95

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

content/5_Plat/Range_Sweep.mdx

+12-9
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int main() {
9797

9898
The first step is to create a DP to solve the first subtask. The states are the springboards and the transitions are between springboards. First, sort the springboards by the pair $(x_1, y_1)$ in increasing order. It is possible to show that for all $i$, $j$, where $i < j$, Bessie cannot use springboard $j$ then $i$ later.
9999

100-
For each springboard $A_i$, let $ans[i]$ denote the minimum distance needed to walk to the start point of springboard $i$.
100+
For each springboard $i$, let $ans[i]$ denote the minimum distance needed to walk to the start point of springboard $i$.
101101

102102
Let $dist(a, b)$ be the walking distance from the end of springboard $a$ and the start of springboard $b$.
103103

@@ -115,7 +115,7 @@ $ans[i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] + x_1
115115

116116
$ans[i] = x_1[i] + y_1[i] + \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] - x_2[j] - y_2[j])$
117117

118-
We notice that everything inside the $\min$ statement only depends on $j$. The segment tree stores $ans[j] - x_2[j] - y_2[j]$ at index $y_2[j]$. We can seperate the start and end of springboards to create two seperate events from them, still sorting by $(x, y)$. When the event is the start of a springboard, update $ans[i]$ through a segment tree query. When the event is the end of a springboard, update the segment tree.
118+
We notice that everything inside the $\min$ statement only depends on $j$. The segment tree stores $ans[j] - x_2[j] - y_2[j]$ at index $y_2[j]$. We can seperate the start and end of springboards to create two seperate events for each springboard, still sorting by $(x, y)$. When the event is the start of a springboard, update $ans[i]$ through a segment tree query. When the event is the end of a springboard, update the segment tree.
119119

120120
By processing in order, the first two conditions in the $\min$ statement are always satisfied. The third is where the segment tree comes into play, where querying the range $[0, y_1[i]]$ is sufficent to satisfy all constraints.
121121

@@ -179,35 +179,38 @@ vi distinct_y;
179179

180180
int query_min(int ind) { return S.query(0, ind); }
181181
void ins(int ind, int val) { S.upd(ind, val); }
182-
int y_index(int y) { return lower_bound(all(distinct_y), y) - begin(distinct_y); }
182+
// gets the coordinate compressed y
183+
int y_index(int y) {
184+
return lower_bound(all(distinct_y), y) - begin(distinct_y);
185+
}
183186

184187
int main() {
185188
ios_base::sync_with_stdio(0); cin.tie(0);
186189
freopen("boards.in", "r", stdin);
187190
freopen("boards.out", "w", stdout);
188191
cin >> N >> P;
189-
vector<pair<pair<int, int>, pair<int, int>>> ev;
192+
vector<pair<pair<int, int>, pair<int, int>>> events;
190193
for (int i = 0; i < P; ++i) {
191194
pair<int, int> a, b;
192195
cin >> a.f >> a.s >> b.f >> b.s;
193-
ev.pb({a, {i, -1}}); // start point
194-
ev.pb({b, {i, 1}}); // end point
196+
events.pb({a, {i, -1}}); // start point
197+
events.pb({b, {i, 1}}); // end point
195198
distinct_y.pb(a.s);
196199
distinct_y.pb(b.s);
197200
}
198201

199202
sort(all(distinct_y));
200-
sort(begin(ev), end(ev));
203+
sort(all(events));
201204
S.init(2 * P);
202205
ins(0, 0);
203-
for (auto &t : ev) {
206+
for (auto &t : events) {
204207
if (t.s.s == -1) {
205208
ans[t.s.f] = t.f.f + t.f.s + query_min(y_index(t.f.s));
206209
} else {
207210
ins(y_index(t.f.s), ans[t.s.f] - t.f.f - t.f.s);
208211
}
209212
}
210-
cout << query_min(2 * P - 1) + 2 * N;
213+
cout << query_min(2 * P - 1) + 2 * N; // adds 2N to include the transition from the last springboard to the endpoint
211214
}
212215
```
213216

0 commit comments

Comments
 (0)