@@ -97,7 +97,7 @@ int main() {
97
97
98
98
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.
99
99
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$.
101
101
102
102
Let $dist(a, b)$ be the walking distance from the end of springboard $a$ and the start of springboard $b$.
103
103
@@ -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
115
115
116
116
$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])$
117
117
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.
119
119
120
120
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.
121
121
@@ -179,35 +179,38 @@ vi distinct_y;
179
179
180
180
int query_min(int ind) { return S.query(0, ind); }
181
181
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
+ }
183
186
184
187
int main() {
185
188
ios_base::sync_with_stdio (0 ); cin .tie (0 );
186
189
freopen (" boards.in" , " r" , stdin );
187
190
freopen (" boards.out" , " w" , stdout );
188
191
cin >> N >> P ;
189
- vector < pair < pair < int , int>, pair<int , int>>> ev ;
192
+ vector < pair < pair < int , int>, pair<int , int>>> events ;
190
193
for (int i = 0 ; i < P ; ++i ) {
191
194
pair < int , int > a , b ;
192
195
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
195
198
distinct_y .pb (a .s );
196
199
distinct_y .pb (b .s );
197
200
}
198
201
199
202
sort(all (distinct_y ));
200
- sort(begin ( ev ), end( ev ));
203
+ sort(all ( events ));
201
204
S.init(2 * P);
202
205
ins(0 , 0 );
203
- for (auto &t : ev ) {
206
+ for (auto &t : events ) {
204
207
if (t.s.s == -1) {
205
208
ans [t .s .f ] = t .f .f + t .f .s + query_min (y_index (t .f .s ));
206
209
} else {
207
210
ins (y_index (t .f .s ), ans [t .s .f ] - t .f .f - t .f .s );
208
211
}
209
212
}
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
211
214
}
212
215
` ` `
213
216
0 commit comments