Submission #1078566

# Submission time Handle Problem Language Result Execution time Memory
1078566 2024-08-27T21:18:04 Z PanosPask Soccer Stadium (IOI23_soccer) C++17
100 / 100
1639 ms 201808 KB
#include "soccer.h"
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pi;

const int INF = 1e6;

struct SegTree {
    int size;
    vector<int> tree;
    const int DEFAULT = INF;

    void init(int N) {
        size = 1;
        while (size < N) {
            size *= 2;
        }

        tree.assign(2 * size, DEFAULT);
    }

    void set(int i, int v, int x, int lx, int rx) {
        if (lx == rx - 1) {
            tree[x] = v;
            return;
        }

        int mid = (lx + rx) / 2;
        if (i < mid) {
            set(i, v, 2 * x + 1, lx, mid);
        }
        else {
            set(i, v, 2 * x + 2, mid, rx);
        }

        tree[x] = min(tree[2 * x + 1], tree[2 * x + 2]);
    }
    void set(int i, int v) {
        set(i, v, 0, 0, size);
    }

    int get(int l, int r, int x, int lx, int rx) {
        if (lx >= r || l >= rx) {
            return DEFAULT;
        }
        else if (l <= lx && rx <= r) {
            return tree[x];
        }

        int mid = (lx + rx) / 2;
        int g1 = get(l, r, 2 * x + 1, lx, mid);
        int g2 = get(l, r, 2 * x + 2, mid, rx);

        return min(g1, g2);
    }
    int get(int l, int r) {
        return get(l, r, 0, 0, size);
    }
};

int N;
vector<vector<int>> grid;
vector<SegTree> above;
vector<SegTree> below;
vector<vector<pi>> segments;

/* dp[i][j][l][r]:
 * Maximum number of added cells if we have already processed rows [i+1, j-1] and now want to use segments fully contained within [l..r)
 */
map<tuple<int, int, int ,int>, int> dp;

void swap(SegTree& a, SegTree& b)
{
    swap(a.size, b.size);
    swap(a.tree, b.tree);
}

void calculate_trees_above(vector<SegTree>& cur)
{
    vector<vector<int>> prevtrees(N, vector<int>(N));

    cur.resize(N);
    for (int r = 0; r < N; r++) {
        cur[r].init(N);
        for (int c = 0; c < N; c++) {
            if (grid[r][c]) {
                prevtrees[r][c] = 0;
            }
            else if (!r) {
                prevtrees[r][c] = 1;
            }
            else {
                prevtrees[r][c] = prevtrees[r - 1][c] + 1;
            }

            cur[r].set(c, prevtrees[r][c]);
        }
    }
}

int calculate(int i, int j, int l, int r)
{
    // Bring the dp to the optimized version
    int up = above[i].get(l, r);
    int down = below[j].get(l, r);
    i = i - up + 1;
    j = j + down - 1;

    int res = (r - l) * (up + down - 1);

    if (dp.count(make_tuple(i, j, l, r))) {
        return dp[make_tuple(i, j, l, r)] + res;
    }

    int opt = 0;

    // Move to row i - 1
    if (i > 0) {
        int last = lower_bound(segments[i - 1].begin(), segments[i - 1].end(), mp(r, -1)) - segments[i - 1].begin() - 1;
        for (int p = last; p >= 0 && segments[i - 1][p].second > l; p--) {
            opt = max(opt, calculate(i - 1, j, max(segments[i - 1][p].first, l), min(segments[i - 1][p].second, r)));
        }
    }

    // Move to row j + 1
    if (j < N - 1) {
        int last = lower_bound(segments[j + 1].begin(), segments[j + 1].end(), mp(r, -1)) - segments[j + 1].begin() - 1;
        for (int p = last; p >= 0 && segments[j + 1][p].second > l; p--) {
            opt = max(opt, calculate(i, j + 1, max(segments[j + 1][p].first, l), min(segments[j + 1][p].second, r)));
        }
    }

    dp[make_tuple(i, j, l, r)] = opt;

    return opt + res;
}

void find_segments(void)
{
    segments.resize(N);

    for (int r = 0; r < N; r++) {
        for (int c = 0; c < N; c++) {
            if (grid[r][c] == 0) {
                if (segments[r].size() == 0 || segments[r].back().second != -1) {
                    segments[r].pb(mp(c, -1));
                }
            }
            else {
                if (segments[r].size() && segments[r].back().second == -1) {
                    segments[r].back().second = c;
                }
            }
        }

        if (segments[r].size() && segments[r].back().second == -1) {
            segments[r].back().second = N;
        }
    }
}

int biggest_stadium(int n, std::vector<std::vector<int>> F)
{
    N = n;
    grid.resize(N, vector<int>(N));

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            grid[i][j] = F[i][j];
        }
    }

    find_segments();
    calculate_trees_above(above);

    reverse(grid.begin(), grid.end());
    calculate_trees_above(below);
    reverse(grid.begin(), grid.end());
    reverse(below.begin(), below.end());

    int ans = 0;
    for (int r = 0; r < N; r++) {
        for (auto s : segments[r]) {
            ans = max(ans, calculate(r, r, s.first, s.second));
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB ok
2 Correct 1 ms 348 KB ok
3 Correct 1 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 1 ms 696 KB ok
8 Correct 29 ms 8960 KB ok
9 Correct 507 ms 134788 KB ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB ok
2 Correct 1 ms 348 KB ok
3 Correct 1 ms 352 KB ok
4 Correct 1 ms 348 KB ok
5 Correct 0 ms 348 KB ok
6 Correct 0 ms 348 KB ok
7 Correct 0 ms 344 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 1 ms 348 KB ok
12 Correct 0 ms 348 KB ok
13 Correct 1 ms 348 KB ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB ok
2 Correct 1 ms 348 KB ok
3 Correct 1 ms 348 KB ok
4 Correct 1 ms 352 KB ok
5 Correct 1 ms 348 KB ok
6 Correct 0 ms 348 KB ok
7 Correct 0 ms 348 KB ok
8 Correct 0 ms 344 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 1 ms 348 KB ok
13 Correct 0 ms 348 KB ok
14 Correct 1 ms 348 KB ok
15 Correct 0 ms 348 KB ok
16 Correct 1 ms 440 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 1 ms 344 KB ok
21 Correct 1 ms 348 KB ok
22 Correct 0 ms 348 KB ok
23 Correct 1 ms 348 KB ok
24 Correct 1 ms 436 KB ok
25 Correct 1 ms 440 KB ok
26 Correct 1 ms 348 KB ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB ok
2 Correct 1 ms 348 KB ok
3 Correct 1 ms 348 KB ok
4 Correct 1 ms 348 KB ok
5 Correct 0 ms 348 KB ok
6 Correct 1 ms 352 KB ok
7 Correct 1 ms 348 KB ok
8 Correct 0 ms 348 KB ok
9 Correct 0 ms 348 KB ok
10 Correct 0 ms 344 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 1 ms 348 KB ok
15 Correct 0 ms 348 KB ok
16 Correct 1 ms 348 KB ok
17 Correct 0 ms 348 KB ok
18 Correct 1 ms 440 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 1 ms 344 KB ok
23 Correct 1 ms 348 KB ok
24 Correct 0 ms 348 KB ok
25 Correct 1 ms 348 KB ok
26 Correct 1 ms 436 KB ok
27 Correct 1 ms 440 KB ok
28 Correct 1 ms 348 KB ok
29 Correct 1 ms 344 KB ok
30 Correct 0 ms 348 KB ok
31 Correct 1 ms 344 KB ok
32 Correct 1 ms 344 KB ok
33 Correct 1 ms 348 KB ok
34 Correct 1 ms 348 KB ok
35 Correct 1 ms 344 KB ok
36 Correct 1 ms 348 KB ok
37 Correct 1 ms 348 KB ok
38 Correct 1 ms 344 KB ok
39 Correct 1 ms 348 KB ok
40 Correct 1 ms 344 KB ok
41 Correct 1 ms 348 KB ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB ok
2 Correct 1 ms 348 KB ok
3 Correct 1 ms 348 KB ok
4 Correct 1 ms 348 KB ok
5 Correct 0 ms 348 KB ok
6 Correct 1 ms 352 KB ok
7 Correct 1 ms 348 KB ok
8 Correct 0 ms 348 KB ok
9 Correct 0 ms 348 KB ok
10 Correct 0 ms 344 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 1 ms 348 KB ok
15 Correct 0 ms 348 KB ok
16 Correct 1 ms 348 KB ok
17 Correct 0 ms 348 KB ok
18 Correct 1 ms 440 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 1 ms 344 KB ok
23 Correct 1 ms 348 KB ok
24 Correct 0 ms 348 KB ok
25 Correct 1 ms 348 KB ok
26 Correct 1 ms 436 KB ok
27 Correct 1 ms 440 KB ok
28 Correct 1 ms 348 KB ok
29 Correct 1 ms 344 KB ok
30 Correct 0 ms 348 KB ok
31 Correct 1 ms 344 KB ok
32 Correct 1 ms 344 KB ok
33 Correct 1 ms 348 KB ok
34 Correct 1 ms 348 KB ok
35 Correct 1 ms 344 KB ok
36 Correct 1 ms 348 KB ok
37 Correct 1 ms 348 KB ok
38 Correct 1 ms 344 KB ok
39 Correct 1 ms 348 KB ok
40 Correct 1 ms 344 KB ok
41 Correct 1 ms 348 KB ok
42 Correct 91 ms 11348 KB ok
43 Correct 94 ms 12288 KB ok
44 Correct 70 ms 8788 KB ok
45 Correct 40 ms 8824 KB ok
46 Correct 71 ms 10112 KB ok
47 Correct 29 ms 8796 KB ok
48 Correct 25 ms 8956 KB ok
49 Correct 27 ms 8796 KB ok
50 Correct 48 ms 9432 KB ok
51 Correct 41 ms 9044 KB ok
52 Correct 48 ms 8788 KB ok
53 Correct 30 ms 8788 KB ok
54 Correct 28 ms 8792 KB ok
55 Correct 32 ms 8744 KB ok
56 Correct 49 ms 8756 KB ok
57 Correct 96 ms 11776 KB ok
58 Correct 70 ms 12372 KB ok
59 Correct 72 ms 12888 KB ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB ok
2 Correct 1 ms 348 KB ok
3 Correct 1 ms 348 KB ok
4 Correct 1 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 1 ms 696 KB ok
9 Correct 29 ms 8960 KB ok
10 Correct 507 ms 134788 KB ok
11 Correct 1 ms 352 KB ok
12 Correct 1 ms 348 KB ok
13 Correct 0 ms 348 KB ok
14 Correct 0 ms 348 KB ok
15 Correct 0 ms 344 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 1 ms 348 KB ok
20 Correct 0 ms 348 KB ok
21 Correct 1 ms 348 KB ok
22 Correct 0 ms 348 KB ok
23 Correct 1 ms 440 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 1 ms 344 KB ok
28 Correct 1 ms 348 KB ok
29 Correct 0 ms 348 KB ok
30 Correct 1 ms 348 KB ok
31 Correct 1 ms 436 KB ok
32 Correct 1 ms 440 KB ok
33 Correct 1 ms 348 KB ok
34 Correct 1 ms 344 KB ok
35 Correct 0 ms 348 KB ok
36 Correct 1 ms 344 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 1 ms 344 KB ok
41 Correct 1 ms 348 KB ok
42 Correct 1 ms 348 KB ok
43 Correct 1 ms 344 KB ok
44 Correct 1 ms 348 KB ok
45 Correct 1 ms 344 KB ok
46 Correct 1 ms 348 KB ok
47 Correct 91 ms 11348 KB ok
48 Correct 94 ms 12288 KB ok
49 Correct 70 ms 8788 KB ok
50 Correct 40 ms 8824 KB ok
51 Correct 71 ms 10112 KB ok
52 Correct 29 ms 8796 KB ok
53 Correct 25 ms 8956 KB ok
54 Correct 27 ms 8796 KB ok
55 Correct 48 ms 9432 KB ok
56 Correct 41 ms 9044 KB ok
57 Correct 48 ms 8788 KB ok
58 Correct 30 ms 8788 KB ok
59 Correct 28 ms 8792 KB ok
60 Correct 32 ms 8744 KB ok
61 Correct 49 ms 8756 KB ok
62 Correct 96 ms 11776 KB ok
63 Correct 70 ms 12372 KB ok
64 Correct 72 ms 12888 KB ok
65 Correct 1639 ms 170772 KB ok
66 Correct 1139 ms 174280 KB ok
67 Correct 679 ms 144132 KB ok
68 Correct 493 ms 132948 KB ok
69 Correct 817 ms 135424 KB ok
70 Correct 1078 ms 143880 KB ok
71 Correct 461 ms 134736 KB ok
72 Correct 436 ms 134920 KB ok
73 Correct 439 ms 134740 KB ok
74 Correct 445 ms 134892 KB ok
75 Correct 490 ms 134740 KB ok
76 Correct 1079 ms 142928 KB ok
77 Correct 1149 ms 142892 KB ok
78 Correct 577 ms 136528 KB ok
79 Correct 505 ms 135808 KB ok
80 Correct 513 ms 135512 KB ok
81 Correct 492 ms 135252 KB ok
82 Correct 574 ms 135380 KB ok
83 Correct 779 ms 141396 KB ok
84 Correct 426 ms 134888 KB ok
85 Correct 443 ms 134708 KB ok
86 Correct 429 ms 134736 KB ok
87 Correct 454 ms 134972 KB ok
88 Correct 441 ms 134744 KB ok
89 Correct 416 ms 134224 KB ok
90 Correct 457 ms 134848 KB ok
91 Correct 451 ms 134740 KB ok
92 Correct 696 ms 136788 KB ok
93 Correct 661 ms 137552 KB ok
94 Correct 545 ms 134716 KB ok
95 Correct 519 ms 134124 KB ok
96 Correct 488 ms 134232 KB ok
97 Correct 474 ms 133940 KB ok
98 Correct 462 ms 133972 KB ok
99 Correct 1395 ms 185336 KB ok
100 Correct 1252 ms 181076 KB ok
101 Correct 1160 ms 174928 KB ok
102 Correct 1181 ms 181072 KB ok
103 Correct 1387 ms 191824 KB ok
104 Correct 1442 ms 195668 KB ok
105 Correct 1430 ms 198600 KB ok
106 Correct 1582 ms 201808 KB ok
107 Correct 1535 ms 201096 KB ok
108 Correct 644 ms 139092 KB ok
109 Correct 732 ms 137044 KB ok