@@ -14,9 +14,9 @@ Similarly to [Maximum Building I](https://cses.fi/problemset/task/1147) apply mo
14
14
- $u_ { i , j } $ , the last line above $i$ with cell $(x, j)$ such that $r_ { x , j } > r_ { i , j } $
15
15
- $d_ { i , j } $ , the last line under $i$ with cell $(y, j)$ such that $r_ { y , j } \ge r_ { i , j } $
16
16
17
- Using [ prefix sums] ( /silver/more-prefix-sums#2d-prefix-sums ) and [ difference arrays] ( https://codeforces.com/blog/entry/78762 )
18
- 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
+ Using [ prefix sums] ( /silver/more-prefix-sums#2d-prefix-sums ) and [ difference arrays] ( https://codeforces.com/blog/entry/78762 )
18
+ 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 } $.
20
20
We'll do difference arrays on each column independently. Accordingly, the updates of the answer matrix look like this:
21
21
- $ans[ 1] [ r_{i,j} ] ++$
22
22
- $ans[ i - u_ { i , j } + 2] [ r_{i,j} ] --$ , the upperbound
@@ -34,76 +34,65 @@ Finally, add $ans[i][j]$ to $ans[i][j-1]$ i.e. a submatrix of size $i \times j$
34
34
35
35
``` cpp
36
36
#include < iostream>
37
- #include < vector>
38
37
#include < stack>
38
+ #include < vector>
39
39
40
40
using namespace std ;
41
41
42
- int main () {
42
+ int main () {
43
43
int n, m;
44
44
cin >> n >> m;
45
45
vector<vector<int>> r(n + 2, vector<int>(m + 2));
46
46
vector<vector<int>> u(n + 2, vector<int>(m + 2));
47
47
vector<vector<int>> d(n + 2, vector<int>(m + 2));
48
48
vector<vector<int>> ans(n + 3, vector<int>(m + 3));
49
49
vector<vector<char>> mat(n + 2, vector<char>(m + 2));
50
- for(int i = 1; i <= n; i++) {
51
- for (int j = 1 ; j <= m ; j ++ ) {
52
- cin >> mat[i ][j ];
53
- }
54
- for(int j = m; j >= 1; j--) {
55
- if(mat[i ][j ] == '*') {
50
+ for (int i = 1; i <= n; i++) {
51
+ for (int j = 1 ; j <= m ; j ++ ) { cin >> mat[i ][j ]; }
52
+ for (int j = m; j >= 1; j--) {
53
+ if (mat[i ][j ] == '*') {
56
54
r[i ][j ] = 0;
57
55
} else {
58
56
r[i ][j ] = r[i ][j + 1 ] + 1;
59
57
}
60
58
}
61
59
}
62
60
stack<int> st;
63
- for(int j = 1; j <= m; j++) {
64
- while(!st.empty()) { st.pop(); }
65
- for(int i = 1; i <= n; i++) {
66
- while(!st.empty() && r[i ][j ] < r [st .top ()][j ]) { st .pop (); }
61
+ for (int j = 1; j <= m; j++) {
62
+ while (!st.empty()) { st.pop(); }
63
+ for (int i = 1; i <= n; i++) {
64
+ while (!st.empty() && r[i ][j ] < r [st .top ()][j ]) { st .pop (); }
67
65
u [i ][j ] = st .empty () ? 1 : (st .top () + 1 );
68
66
st .push (i );
69
67
}
70
- while (!st .empty ()) { st .pop (); }
71
- for (int i = n ; i >= 1 ; i --) {
72
- while(!st.empty() && r[i][j ] <= r [st .top ()][j ]) { st .pop (); }
68
+ while (!st .empty ()) { st .pop (); }
69
+ for (int i = n ; i >= 1 ; i --) {
70
+ while (!st.empty() && r[i][j ] <= r [st .top ()][j ]) { st .pop (); }
73
71
d [i ][j ] = st .empty () ? n : (st .top () - 1 );
74
72
st .push (i );
75
73
}
76
74
}
77
- for (int i = 1 ; i <= n ; i ++) {
78
- for(int j = 1 ; j <= m ; j ++) {
75
+ for (int i = 1 ; i <= n ; i ++) {
76
+ for (int j = 1 ; j <= m ; j ++) {
79
77
ans [1 ][r [i ][j ]]++ ;
80
78
ans [i - u [i ][j ] + 2 ][r [i ][j ]]-- ;
81
79
ans [d [i ][j ] - i + 2 ][r [i ][j ]]-- ;
82
80
ans [d [i ][j ] - u [i ][j ] + 3 ][r [i ][j ]]++ ;
83
81
}
84
82
}
85
- for (int j = 1 ; j <= m ; ++j ) {
86
- for(int i = 1 ; i <= n ; ++i ) {
87
- ans [i ][j ] += ans [i - 1 ][j ];
88
- }
89
- for(int i = 1 ; i <= n ; ++i ) {
90
- ans [i ][j ] += ans [i - 1 ][j ];
91
- }
83
+ for (int j = 1 ; j <= m ; ++j ) {
84
+ for (int i = 1 ; i <= n ; ++i ) { ans [i ][j ] += ans [i - 1 ][j ]; }
85
+ for (int i = 1 ; i <= n ; ++i ) { ans [i ][j ] += ans [i - 1 ][j ]; }
92
86
}
93
- for (int i = n ; i >= 1 ; --i ) {
94
- for(int j = m ; j >= 2 ; --j ) {
95
- ans [i ][j - 1 ] += ans [i ][j ];
96
- }
87
+ for (int i = n ; i >= 1 ; --i ) {
88
+ for (int j = m ; j >= 2 ; --j ) { ans [i ][j - 1 ] += ans [i ][j ]; }
97
89
}
98
- for (int i = 1 ; i <= n ; ++i ) {
99
- for(int j = 1 ; j <= m ; ++j ) {
100
- cout << ans [i ][j ] << " " ;
101
- }
90
+ for (int i = 1 ; i <= n ; ++i ) {
91
+ for (int j = 1 ; j <= m ; ++j ) { cout << ans [i ][j ] << " " ; }
102
92
cout << ' \n ' ;
103
93
}
104
94
}
105
-
106
95
` ` `
107
96
108
97
</CPPSection>
109
- </LanguageSection>
98
+ </LanguageSection>
0 commit comments