@@ -3,8 +3,6 @@ id: cses-1148
3
3
source : CSES
4
4
title : Maximum Building II
5
5
author : Mihnea Brebenel
6
- prerequisites :
7
- - prefix-sums
8
6
---
9
7
10
8
## Explanation
@@ -16,7 +14,7 @@ Similarly to [Maximum Building I](https://cses.fi/problemset/task/1147) apply mo
16
14
17
15
Using [ prefix sums] ( /silver/more-prefix-sums#2d-prefix-sums ) and [ difference arrays] ( https://codeforces.com/blog/entry/78762 )
18
16
we can efficiently update the answer matrix. Having $r_ { i ,j } $, $u_ { i ,j } $ and $d_ { i ,j } $ precomputed, we know the upperbound
19
- and the lowerbound of the rectangle of of width $r_ { i ,j } $ i.e. how much it can expand above and below line $i$ maintaing width $r_ { i , j } $.
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 } $.
20
18
We'll do difference arrays on each column independently. Accordingly, the updates of the answer matrix look like this:
21
19
- $ans[ 1] [ r_{i,j} ] ++$
22
20
- $ans[ i - u_ { i , j } + 2] [ r_{i,j} ] --$ , the upperbound
@@ -58,6 +56,7 @@ int main() {
58
56
}
59
57
}
60
58
stack<int> st;
59
+ // Precompute u[i][j] and d[i][j]
61
60
for (int j = 1; j <= m; j++) {
62
61
while (!st.empty()) { st.pop(); }
63
62
for (int i = 1; i <= n; i++) {
@@ -72,6 +71,7 @@ int main() {
72
71
st .push (i );
73
72
}
74
73
}
74
+ // Make difference array on each column independently
75
75
for (int i = 1 ; i <= n ; i ++) {
76
76
for (int j = 1 ; j <= m ; j ++) {
77
77
ans [1 ][r [i ][j ]]++ ;
0 commit comments