Skip to content

Commit 83850e7

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

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

solutions/gold/cses-1148.mdx

+26-37
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Similarly to [Maximum Building I](https://cses.fi/problemset/task/1147) apply mo
1414
- $u_{i, j}$ , the last line above $i$ with cell $(x, j)$ such that $r_{x, j} > r_{i, j}$
1515
- $d_{i, j}$ , the last line under $i$ with cell $(y, j)$ such that $r_{y, j} \ge r_{i, j}$
1616

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}$.
2020
We'll do difference arrays on each column independently. Accordingly, the updates of the answer matrix look like this:
2121
- $ans[1][r_{i,j}]++$
2222
- $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$
3434

3535
```cpp
3636
#include <iostream>
37-
#include <vector>
3837
#include <stack>
38+
#include <vector>
3939

4040
using namespace std;
4141

42-
int main() {
42+
int main() {
4343
int n, m;
4444
cin >> n >> m;
4545
vector<vector<int>> r(n + 2, vector<int>(m + 2));
4646
vector<vector<int>> u(n + 2, vector<int>(m + 2));
4747
vector<vector<int>> d(n + 2, vector<int>(m + 2));
4848
vector<vector<int>> ans(n + 3, vector<int>(m + 3));
4949
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] == '*') {
5654
r[i][j] = 0;
5755
} else {
5856
r[i][j] = r[i][j + 1] + 1;
5957
}
6058
}
6159
}
6260
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(); }
6765
u[i][j] = st.empty() ? 1 : (st.top() + 1);
6866
st.push(i);
6967
}
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(); }
7371
d[i][j] = st.empty() ? n : (st.top() - 1);
7472
st.push(i);
7573
}
7674
}
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++) {
7977
ans[1][r[i][j]]++;
8078
ans[i - u[i][j] + 2][r[i][j]]--;
8179
ans[d[i][j] - i + 2][r[i][j]]--;
8280
ans[d[i][j] - u[i][j] + 3][r[i][j]]++;
8381
}
8482
}
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]; }
9286
}
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]; }
9789
}
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] << " "; }
10292
cout << '\n';
10393
}
10494
}
105-
10695
```
10796
10897
</CPPSection>
109-
</LanguageSection>
98+
</LanguageSection>

0 commit comments

Comments
 (0)