Skip to content

Commit b636eb8

Browse files
Update cses-1148.mdx
1 parent 8fb9d75 commit b636eb8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

solutions/gold/cses-1148.mdx

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ Similarly to [Maximum Building I](https://cses.fi/problemset/task/1147) apply mo
1212
- $u_{i, j}$ , the last line above $i$ with cell $(x, j)$ such that $r_{x, j} > r_{i, j}$
1313
- $d_{i, j}$ , the last line under $i$ with cell $(y, j)$ such that $r_{y, j} \ge r_{i, j}$
1414

15-
Using [prefix sums](/silver/more-prefix-sums#2d-prefix-sums) and [difference arrays](https://codeforces.com/blog/entry/78762)
16-
we can efficiently update the answer matrix. Having $r_{i,j}$, $u_{i,j}$ and $d_{i,j}$ precomputed, we know the upperbound
17-
and the lowerbound of the rectangle of width $r_{i,j}$ i.e. how much it can expand above and below line $i$ maintaing width $r_{i, j}$.
18-
We'll do difference arrays on each column independently. Accordingly, the updates of the answer matrix look like this:
15+
We can efficiently update the answer matrix with [prefix sums](/silver/more-prefix-sums#2d-prefix-sums)
16+
and [difference arrays](https://codeforces.com/blog/entry/78762).
17+
Having $r_{i,j}$, $u_{i,j}$ and $d_{i,j}$ precomputed, we know the upper bound
18+
and the lower bound of the rectangle of width $r_{i,j}$,
19+
i.e. how much it can expand above and below line $i$ maintaing width $r_{i, j}$.
20+
21+
We'll do difference arrays on each column independently.
22+
Accordingly, the updates of the answer matrix look like this:
1923
- $ans[1][r_{i,j}]++$
2024
- $ans[i - u_{i, j} + 2][r_{i,j}]--$ , the upperbound
2125
- $ans[d_{i,j} - i + 2][r_{i, j}]]--$ , the lowerbound
2226
- $ans[d_{i,j} - u_{i,j} + 3][r_{i,j}]++$
2327

24-
Finally, add $ans[i][j]$ to $ans[i][j-1]$ i.e. a submatrix of size $i \times j$ contains a submatrix of size $i \times (j-1)$.
28+
Finally, we add $ans[i][j]$ to $ans[i][j-1]$ i.e. a submatrix of size
29+
$i \times j$ contains a submatrix of size $i \times (j-1)$.
2530

2631
## Implementation
2732

@@ -55,6 +60,7 @@ int main() {
5560
}
5661
}
5762
}
63+
5864
stack<int> st;
5965
// Precompute u[i][j] and d[i][j]
6066
for (int j = 1; j <= m; j++) {
@@ -71,6 +77,7 @@ int main() {
7177
st.push(i);
7278
}
7379
}
80+
7481
// Make difference array on each column independently
7582
for (int i = 1; i <= n; i++) {
7683
for (int j = 1; j <= m; j++) {
@@ -87,6 +94,7 @@ int main() {
8794
for (int i = n; i >= 1; --i) {
8895
for (int j = m; j >= 2; --j) { ans[i][j - 1] += ans[i][j]; }
8996
}
97+
9098
for (int i = 1; i <= n; ++i) {
9199
for (int j = 1; j <= m; ++j) { cout << ans[i][j] << " "; }
92100
cout << '\n';

0 commit comments

Comments
 (0)