@@ -12,16 +12,21 @@ Similarly to [Maximum Building I](https://cses.fi/problemset/task/1147) apply mo
12
12
- $u_ { i , j } $ , the last line above $i$ with cell $(x, j)$ such that $r_ { x , j } > r_ { i , j } $
13
13
- $d_ { i , j } $ , the last line under $i$ with cell $(y, j)$ such that $r_ { y , j } \ge r_ { i , j } $
14
14
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:
19
23
- $ans[ 1] [ r_{i,j} ] ++$
20
24
- $ans[ i - u_ { i , j } + 2] [ r_{i,j} ] --$ , the upperbound
21
25
- $ans[ d_ { i ,j } - i + 2] [ r_{i, j} ]] --$ , the lowerbound
22
26
- $ans[ d_ { i ,j } - u_ { i ,j } + 3] [ r_{i,j} ] ++$
23
27
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)$.
25
30
26
31
## Implementation
27
32
@@ -55,6 +60,7 @@ int main() {
55
60
}
56
61
}
57
62
}
63
+
58
64
stack<int> st;
59
65
// Precompute u[i][j] and d[i][j]
60
66
for (int j = 1; j <= m; j++) {
@@ -71,6 +77,7 @@ int main() {
71
77
st .push (i );
72
78
}
73
79
}
80
+
74
81
// Make difference array on each column independently
75
82
for (int i = 1 ; i <= n ; i ++) {
76
83
for (int j = 1 ; j <= m ; j ++) {
@@ -87,6 +94,7 @@ int main() {
87
94
for (int i = n ; i >= 1 ; --i ) {
88
95
for (int j = m ; j >= 2 ; --j ) { ans [i ][j - 1 ] += ans [i ][j ]; }
89
96
}
97
+
90
98
for (int i = 1 ; i <= n ; ++i ) {
91
99
for (int j = 1 ; j <= m ; ++j ) { cout << ans [i ][j ] << " " ; }
92
100
cout << ' \n ' ;
0 commit comments