#include <bits/stdc++.h>
using namespace std;
template <typename T, class F = function<T(const T&, const T&)>>
class SparseTable {
public:
int n;
vector<vector<T>> mat;
F func;
SparseTable(){};
SparseTable(const vector<T>& a, const F& f) : func(f) {
n = static_cast<int>(a.size());
int max_log = 32 - __builtin_clz(n);
mat.resize(max_log);
mat[0] = a;
for (int j = 1; j < max_log; j++) {
mat[j].resize(n - (1 << j) + 1);
for (int i = 0; i <= n - (1 << j); i++) {
mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
}
}
}
T get(int from, int to) const {
assert(0 <= from && from <= to && to <= n - 1);
int lg = 32 - __builtin_clz(to - from + 1) - 1;
return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]);
}
};
int biggest_stadium(int n, vector<vector<int>> F) {
vector<vector<int>> row(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (F[i][j]) {
row[i].push_back(j);
}
}
}
vector<SparseTable<int>> Lrec(n);
vector<SparseTable<int>> Hrec(n);
vector<int> cur(n, -1);
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
if (F[r][c]) cur[c] = r;
}
Hrec[r] = SparseTable<int>(cur, [&](int i, int j){ return max(i, j); });
}
cur = vector<int>(n, n);
for (int r = n - 1; r >= 0; r--) {
for (int c = 0; c < n; c++) {
if (F[r][c]) cur[c] = r;
}
Lrec[r] = SparseTable<int>(cur, [&](int i, int j){ return min(i, j); });
}
auto get_rec = [&](int y, int Lx, int Rx) {
int Ly = Hrec[y].get(Lx, Rx) + 1;
int Ry = Lrec[y].get(Lx, Rx) - 1;
return make_tuple(Lx, Rx, Ly, Ry);
};
int ans = 0;
map<tuple<int, int, int, int>, int> dp;
function<int(int, int, int, int)> dfs = [&](int Lx, int Rx, int Ly, int Ry) {
if (dp.find(make_tuple(Lx, Rx, Ly, Ry)) != dp.end()) return dp[make_tuple(Lx, Rx, Ly, Ry)];
int ret = (Rx - Lx + 1) * (Ry - Ly + 1);
auto solve_row = [&](int y) {
vector<int> que;
for (auto it = lower_bound(row[y].begin(), row[y].end(), Lx); it != row[y].end() && *it <= Rx; it++) {
que.push_back(*it);
}
assert(que.size());
if (que[0] > Lx) {
auto [lx, rx, ly, ry] = get_rec(y, Lx, que[0] - 1);
int add = (Ry - Ly + 1) * (Rx - Lx - (rx - lx));
ret = max(ret, add + dfs(lx, rx, ly, ry));
}
for (int i = 0; i < (int) que.size() - 1; i++) {
if (que[i + 1] - que[i] == 1) continue;
auto [lx, rx, ly, ry] = get_rec(y, que[i] + 1, que[i + 1] - 1);
int add = (Ry - Ly + 1) * (Rx - Lx - (rx - lx));
ret = max(ret, add + dfs(lx, rx, ly, ry));
}
if (que.back() < Rx) {
auto [lx, rx, ly, ry] = get_rec(y, que.back() + 1, Rx);
int add = (Ry - Ly + 1) * (Rx - Lx - (rx - lx));
ret = max(ret, add + dfs(lx, rx, ly, ry));
}
};
if (Ly > 0) {
solve_row(Ly - 1);
}
if (Ry < n - 1) {
solve_row(Ry + 1);
}
return dp[make_tuple(Lx, Rx, Ly, Ry)] = ret;
};
for (int r = 0; r < n; r++) {
vector<int> cur = {-1};
for (int c : row[r]) cur.push_back(c);
cur.push_back(n);
int sz = cur.size();
for (int i = 0; i < sz - 1; i++) {
int L = cur[i], R = cur[i + 1];
if (R == L + 1) continue;
auto [Lx, Rx, Ly, Ry] = get_rec(r, L + 1, R - 1);
assert(Lx == L + 1 && Rx == R - 1 && Ly <= r && Ry >= r);
ans = max(ans, dfs(Lx, Rx, Ly, Ry));
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
596 KB |
ok |
2 |
Correct |
0 ms |
348 KB |
ok |
3 |
Correct |
0 ms |
348 KB |
ok |
4 |
Correct |
0 ms |
348 KB |
ok |
5 |
Correct |
0 ms |
348 KB |
ok |
6 |
Correct |
0 ms |
440 KB |
ok |
7 |
Correct |
1 ms |
960 KB |
ok |
8 |
Correct |
25 ms |
18764 KB |
ok |
9 |
Correct |
442 ms |
354068 KB |
ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
596 KB |
ok |
2 |
Correct |
0 ms |
348 KB |
ok |
3 |
Correct |
0 ms |
348 KB |
ok |
4 |
Correct |
0 ms |
348 KB |
ok |
5 |
Correct |
0 ms |
348 KB |
ok |
6 |
Correct |
0 ms |
348 KB |
ok |
7 |
Correct |
0 ms |
348 KB |
ok |
8 |
Correct |
0 ms |
348 KB |
ok |
9 |
Correct |
0 ms |
600 KB |
ok |
10 |
Correct |
0 ms |
348 KB |
ok |
11 |
Correct |
0 ms |
348 KB |
ok |
12 |
Correct |
0 ms |
348 KB |
ok |
13 |
Correct |
0 ms |
344 KB |
ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
ok |
2 |
Correct |
0 ms |
596 KB |
ok |
3 |
Correct |
0 ms |
348 KB |
ok |
4 |
Correct |
0 ms |
348 KB |
ok |
5 |
Correct |
0 ms |
348 KB |
ok |
6 |
Correct |
0 ms |
348 KB |
ok |
7 |
Correct |
0 ms |
348 KB |
ok |
8 |
Correct |
0 ms |
348 KB |
ok |
9 |
Correct |
0 ms |
348 KB |
ok |
10 |
Correct |
0 ms |
600 KB |
ok |
11 |
Correct |
0 ms |
348 KB |
ok |
12 |
Correct |
0 ms |
348 KB |
ok |
13 |
Correct |
0 ms |
348 KB |
ok |
14 |
Correct |
0 ms |
344 KB |
ok |
15 |
Correct |
0 ms |
348 KB |
ok |
16 |
Correct |
0 ms |
348 KB |
ok |
17 |
Correct |
0 ms |
348 KB |
ok |
18 |
Correct |
0 ms |
348 KB |
ok |
19 |
Correct |
0 ms |
348 KB |
ok |
20 |
Correct |
0 ms |
348 KB |
ok |
21 |
Correct |
1 ms |
348 KB |
ok |
22 |
Correct |
1 ms |
348 KB |
ok |
23 |
Correct |
0 ms |
344 KB |
ok |
24 |
Correct |
1 ms |
348 KB |
ok |
25 |
Correct |
0 ms |
348 KB |
ok |
26 |
Correct |
0 ms |
348 KB |
ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
ok |
2 |
Correct |
0 ms |
596 KB |
ok |
3 |
Correct |
0 ms |
348 KB |
ok |
4 |
Correct |
0 ms |
348 KB |
ok |
5 |
Correct |
0 ms |
348 KB |
ok |
6 |
Correct |
0 ms |
348 KB |
ok |
7 |
Correct |
0 ms |
348 KB |
ok |
8 |
Correct |
0 ms |
348 KB |
ok |
9 |
Correct |
0 ms |
348 KB |
ok |
10 |
Correct |
0 ms |
348 KB |
ok |
11 |
Correct |
0 ms |
348 KB |
ok |
12 |
Correct |
0 ms |
600 KB |
ok |
13 |
Correct |
0 ms |
348 KB |
ok |
14 |
Correct |
0 ms |
348 KB |
ok |
15 |
Correct |
0 ms |
348 KB |
ok |
16 |
Correct |
0 ms |
344 KB |
ok |
17 |
Correct |
0 ms |
348 KB |
ok |
18 |
Correct |
0 ms |
348 KB |
ok |
19 |
Correct |
0 ms |
348 KB |
ok |
20 |
Correct |
0 ms |
348 KB |
ok |
21 |
Correct |
0 ms |
348 KB |
ok |
22 |
Correct |
0 ms |
348 KB |
ok |
23 |
Correct |
1 ms |
348 KB |
ok |
24 |
Correct |
1 ms |
348 KB |
ok |
25 |
Correct |
0 ms |
344 KB |
ok |
26 |
Correct |
1 ms |
348 KB |
ok |
27 |
Correct |
0 ms |
348 KB |
ok |
28 |
Correct |
0 ms |
348 KB |
ok |
29 |
Correct |
1 ms |
344 KB |
ok |
30 |
Correct |
0 ms |
348 KB |
ok |
31 |
Correct |
1 ms |
348 KB |
ok |
32 |
Correct |
1 ms |
428 KB |
ok |
33 |
Correct |
0 ms |
348 KB |
ok |
34 |
Correct |
0 ms |
348 KB |
ok |
35 |
Correct |
0 ms |
348 KB |
ok |
36 |
Correct |
0 ms |
348 KB |
ok |
37 |
Correct |
1 ms |
344 KB |
ok |
38 |
Correct |
1 ms |
348 KB |
ok |
39 |
Correct |
1 ms |
348 KB |
ok |
40 |
Correct |
0 ms |
348 KB |
ok |
41 |
Correct |
0 ms |
344 KB |
ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
ok |
2 |
Correct |
0 ms |
596 KB |
ok |
3 |
Correct |
0 ms |
348 KB |
ok |
4 |
Correct |
0 ms |
348 KB |
ok |
5 |
Correct |
0 ms |
348 KB |
ok |
6 |
Correct |
0 ms |
348 KB |
ok |
7 |
Correct |
0 ms |
348 KB |
ok |
8 |
Correct |
0 ms |
348 KB |
ok |
9 |
Correct |
0 ms |
348 KB |
ok |
10 |
Correct |
0 ms |
348 KB |
ok |
11 |
Correct |
0 ms |
348 KB |
ok |
12 |
Correct |
0 ms |
600 KB |
ok |
13 |
Correct |
0 ms |
348 KB |
ok |
14 |
Correct |
0 ms |
348 KB |
ok |
15 |
Correct |
0 ms |
348 KB |
ok |
16 |
Correct |
0 ms |
344 KB |
ok |
17 |
Correct |
0 ms |
348 KB |
ok |
18 |
Correct |
0 ms |
348 KB |
ok |
19 |
Correct |
0 ms |
348 KB |
ok |
20 |
Correct |
0 ms |
348 KB |
ok |
21 |
Correct |
0 ms |
348 KB |
ok |
22 |
Correct |
0 ms |
348 KB |
ok |
23 |
Correct |
1 ms |
348 KB |
ok |
24 |
Correct |
1 ms |
348 KB |
ok |
25 |
Correct |
0 ms |
344 KB |
ok |
26 |
Correct |
1 ms |
348 KB |
ok |
27 |
Correct |
0 ms |
348 KB |
ok |
28 |
Correct |
0 ms |
348 KB |
ok |
29 |
Correct |
1 ms |
344 KB |
ok |
30 |
Correct |
0 ms |
348 KB |
ok |
31 |
Correct |
1 ms |
348 KB |
ok |
32 |
Correct |
1 ms |
428 KB |
ok |
33 |
Correct |
0 ms |
348 KB |
ok |
34 |
Correct |
0 ms |
348 KB |
ok |
35 |
Correct |
0 ms |
348 KB |
ok |
36 |
Correct |
0 ms |
348 KB |
ok |
37 |
Correct |
1 ms |
344 KB |
ok |
38 |
Correct |
1 ms |
348 KB |
ok |
39 |
Correct |
1 ms |
348 KB |
ok |
40 |
Correct |
0 ms |
348 KB |
ok |
41 |
Correct |
0 ms |
344 KB |
ok |
42 |
Correct |
73 ms |
22064 KB |
ok |
43 |
Correct |
100 ms |
22980 KB |
ok |
44 |
Correct |
40 ms |
19796 KB |
ok |
45 |
Correct |
34 ms |
19352 KB |
ok |
46 |
Correct |
64 ms |
21072 KB |
ok |
47 |
Correct |
26 ms |
18768 KB |
ok |
48 |
Correct |
31 ms |
19580 KB |
ok |
49 |
Correct |
29 ms |
19548 KB |
ok |
50 |
Correct |
36 ms |
19064 KB |
ok |
51 |
Correct |
39 ms |
20056 KB |
ok |
52 |
Correct |
29 ms |
19796 KB |
ok |
53 |
Correct |
29 ms |
19804 KB |
ok |
54 |
Correct |
30 ms |
19804 KB |
ok |
55 |
Correct |
29 ms |
19804 KB |
ok |
56 |
Correct |
29 ms |
19548 KB |
ok |
57 |
Correct |
55 ms |
22908 KB |
ok |
58 |
Correct |
60 ms |
23464 KB |
ok |
59 |
Correct |
69 ms |
24148 KB |
ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
ok |
2 |
Correct |
0 ms |
596 KB |
ok |
3 |
Correct |
0 ms |
348 KB |
ok |
4 |
Correct |
0 ms |
348 KB |
ok |
5 |
Correct |
0 ms |
348 KB |
ok |
6 |
Correct |
0 ms |
348 KB |
ok |
7 |
Correct |
0 ms |
440 KB |
ok |
8 |
Correct |
1 ms |
960 KB |
ok |
9 |
Correct |
25 ms |
18764 KB |
ok |
10 |
Correct |
442 ms |
354068 KB |
ok |
11 |
Correct |
0 ms |
348 KB |
ok |
12 |
Correct |
0 ms |
348 KB |
ok |
13 |
Correct |
0 ms |
348 KB |
ok |
14 |
Correct |
0 ms |
348 KB |
ok |
15 |
Correct |
0 ms |
348 KB |
ok |
16 |
Correct |
0 ms |
348 KB |
ok |
17 |
Correct |
0 ms |
600 KB |
ok |
18 |
Correct |
0 ms |
348 KB |
ok |
19 |
Correct |
0 ms |
348 KB |
ok |
20 |
Correct |
0 ms |
348 KB |
ok |
21 |
Correct |
0 ms |
344 KB |
ok |
22 |
Correct |
0 ms |
348 KB |
ok |
23 |
Correct |
0 ms |
348 KB |
ok |
24 |
Correct |
0 ms |
348 KB |
ok |
25 |
Correct |
0 ms |
348 KB |
ok |
26 |
Correct |
0 ms |
348 KB |
ok |
27 |
Correct |
0 ms |
348 KB |
ok |
28 |
Correct |
1 ms |
348 KB |
ok |
29 |
Correct |
1 ms |
348 KB |
ok |
30 |
Correct |
0 ms |
344 KB |
ok |
31 |
Correct |
1 ms |
348 KB |
ok |
32 |
Correct |
0 ms |
348 KB |
ok |
33 |
Correct |
0 ms |
348 KB |
ok |
34 |
Correct |
1 ms |
344 KB |
ok |
35 |
Correct |
0 ms |
348 KB |
ok |
36 |
Correct |
1 ms |
348 KB |
ok |
37 |
Correct |
1 ms |
428 KB |
ok |
38 |
Correct |
0 ms |
348 KB |
ok |
39 |
Correct |
0 ms |
348 KB |
ok |
40 |
Correct |
0 ms |
348 KB |
ok |
41 |
Correct |
0 ms |
348 KB |
ok |
42 |
Correct |
1 ms |
344 KB |
ok |
43 |
Correct |
1 ms |
348 KB |
ok |
44 |
Correct |
1 ms |
348 KB |
ok |
45 |
Correct |
0 ms |
348 KB |
ok |
46 |
Correct |
0 ms |
344 KB |
ok |
47 |
Correct |
73 ms |
22064 KB |
ok |
48 |
Correct |
100 ms |
22980 KB |
ok |
49 |
Correct |
40 ms |
19796 KB |
ok |
50 |
Correct |
34 ms |
19352 KB |
ok |
51 |
Correct |
64 ms |
21072 KB |
ok |
52 |
Correct |
26 ms |
18768 KB |
ok |
53 |
Correct |
31 ms |
19580 KB |
ok |
54 |
Correct |
29 ms |
19548 KB |
ok |
55 |
Correct |
36 ms |
19064 KB |
ok |
56 |
Correct |
39 ms |
20056 KB |
ok |
57 |
Correct |
29 ms |
19796 KB |
ok |
58 |
Correct |
29 ms |
19804 KB |
ok |
59 |
Correct |
30 ms |
19804 KB |
ok |
60 |
Correct |
29 ms |
19804 KB |
ok |
61 |
Correct |
29 ms |
19548 KB |
ok |
62 |
Correct |
55 ms |
22908 KB |
ok |
63 |
Correct |
60 ms |
23464 KB |
ok |
64 |
Correct |
69 ms |
24148 KB |
ok |
65 |
Correct |
1891 ms |
406724 KB |
ok |
66 |
Correct |
1376 ms |
417360 KB |
ok |
67 |
Correct |
827 ms |
391180 KB |
ok |
68 |
Correct |
521 ms |
356632 KB |
ok |
69 |
Correct |
858 ms |
368404 KB |
ok |
70 |
Correct |
1194 ms |
378260 KB |
ok |
71 |
Correct |
522 ms |
355468 KB |
ok |
72 |
Correct |
506 ms |
354208 KB |
ok |
73 |
Correct |
487 ms |
364628 KB |
ok |
74 |
Correct |
488 ms |
364628 KB |
ok |
75 |
Correct |
472 ms |
364696 KB |
ok |
76 |
Correct |
750 ms |
358224 KB |
ok |
77 |
Correct |
734 ms |
358224 KB |
ok |
78 |
Correct |
583 ms |
366184 KB |
ok |
79 |
Correct |
557 ms |
374520 KB |
ok |
80 |
Correct |
536 ms |
373072 KB |
ok |
81 |
Correct |
501 ms |
373516 KB |
ok |
82 |
Correct |
564 ms |
377112 KB |
ok |
83 |
Correct |
842 ms |
389204 KB |
ok |
84 |
Correct |
503 ms |
369816 KB |
ok |
85 |
Correct |
474 ms |
369748 KB |
ok |
86 |
Correct |
495 ms |
369236 KB |
ok |
87 |
Correct |
513 ms |
370152 KB |
ok |
88 |
Correct |
446 ms |
364512 KB |
ok |
89 |
Correct |
441 ms |
356180 KB |
ok |
90 |
Correct |
473 ms |
362132 KB |
ok |
91 |
Correct |
483 ms |
357716 KB |
ok |
92 |
Correct |
654 ms |
381848 KB |
ok |
93 |
Correct |
692 ms |
384020 KB |
ok |
94 |
Correct |
558 ms |
374616 KB |
ok |
95 |
Correct |
513 ms |
372564 KB |
ok |
96 |
Correct |
517 ms |
371724 KB |
ok |
97 |
Correct |
485 ms |
371220 KB |
ok |
98 |
Correct |
470 ms |
370828 KB |
ok |
99 |
Correct |
1116 ms |
420760 KB |
ok |
100 |
Correct |
1146 ms |
417420 KB |
ok |
101 |
Correct |
831 ms |
411284 KB |
ok |
102 |
Correct |
848 ms |
417416 KB |
ok |
103 |
Correct |
1244 ms |
428172 KB |
ok |
104 |
Correct |
1000 ms |
431952 KB |
ok |
105 |
Correct |
1062 ms |
435080 KB |
ok |
106 |
Correct |
1671 ms |
438156 KB |
ok |
107 |
Correct |
1721 ms |
437640 KB |
ok |
108 |
Correct |
511 ms |
362064 KB |
ok |
109 |
Correct |
558 ms |
362068 KB |
ok |