Skip to content

Commit ca1df0f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent aec6cf7 commit ca1df0f

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

content/5_Plat/Range_Sweep.mdx

+12-8
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ $\texttt{dist}(a, b) = x_1[b] + y_1[b] - x_2[a] - y_2[a]$
105105

106106
Then, the transitions are:
107107

108-
$\texttt{ans}[i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] + \texttt{dist}(j, i))$
108+
$\texttt{ans}[i] = \min\limits_{j < i, x_2[j] \le x_1[i], y_2[j] \le y_1[i]}(ans[j] + \texttt{dist}(j, i))$
109109

110110
**Full Solution:** $\mathcal{O}(P \log P)$
111111

112112
Optimizing the DP involves the use of a min point update range query segment tree. Let's first expand $dist(i, j)$ in the transition formula.
113113

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

116-
$\texttt{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]}(\texttt{ans}[j] - x_2[j] - y_2[j])$
116+
$\texttt{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]}(\texttt{ans}[j] - x_2[j] - y_2[j])$
117117

118118
We notice that everything inside the $\min$ statement only depends on $j$. The segment tree stores $\texttt{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

@@ -127,15 +127,16 @@ using namespace std;
127127
#define f first
128128
#define s second
129129
#define pb push_back
130-
#define rep(i, a, b) for(int i = a; i < (b); ++i)
130+
#define rep(i, a, b) for (int i = a; i < (b); ++i)
131131
#define all(x) begin(x), end(x)
132132
#define sz(x) (int)(x).size()
133133
typedef long long ll;
134134
typedef pair<int, int> pii;
135135
typedef vector<int> vi;
136136

137-
template<class T> bool ckmin(T& a, const T& b) {
138-
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
137+
template <class T> bool ckmin(T &a, const T &b) {
138+
return b < a ? a = b, 1 : 0;
139+
} // set a = min(a,b)
139140

140141
/**
141142
* Description: 1D point update, range query where \texttt{comb} is
@@ -185,7 +186,8 @@ int y_index(int y) {
185186
}
186187

187188
int main() {
188-
ios_base::sync_with_stdio(0); cin.tie(0);
189+
ios_base::sync_with_stdio(0);
190+
cin.tie(0);
189191
freopen("boards.in", "r", stdin);
190192
freopen("boards.out", "w", stdout);
191193
cin >> N >> P;
@@ -210,7 +212,9 @@ int main() {
210212
ins(y_index(t.f.s), ans[t.s.f] - t.f.f - t.f.s);
211213
}
212214
}
213-
cout << query_min(2 * P - 1) + 2 * N; // adds 2N to include the transition from the last springboard to the endpoint
215+
cout << query_min(2 * P - 1) +
216+
2 * N; // adds 2N to include the transition from the last
217+
// springboard to the endpoint
214218
}
215219
```
216220

0 commit comments

Comments
 (0)