Submission #549620

# Submission time Handle Problem Language Result Execution time Memory
549620 2022-04-16T07:12:01 Z smax Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
508 ms 129216 KB
#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;

const int MAX = 1e7 + 5;
const int MAXC = 2e5 + 5;

struct Node {
    int vertices, horiEdges, vertEdges, river;

    friend Node operator + (const Node &a, const Node &b) {
        return {a.vertices + b.vertices, a.horiEdges + b.horiEdges, a.vertEdges + b.vertEdges, a.river + b.river};
    }

    friend Node operator - (const Node &a, const Node &b) {
        return {a.vertices - b.vertices, a.horiEdges - b.horiEdges, a.vertEdges - b.vertEdges, a.river - b.river};
    }
};

const Node null{0, 0, 0, 0};
Node st[MAX];
int id, pl[MAX], pr[MAX];

int newLeaf(const Node &cur) {
    int p = id++;
    st[p] = cur;
    pl[p] = pr[p] = 0;
    return p;
}

int newParent(int a, int b) {
    int p = id++;
    st[p] = st[a] + st[b];
    pl[p] = a;
    pr[p] = b;
    return p;
}

Node query(int p1, int p2, int l, int r, int i, int j) {
    if (i > r || j < l)
        return null;
    if (i <= l && r <= j)
        return st[p2] - st[p1];
    int m = (l + r) / 2;
    return query(pl[p1], pl[p2], l, m, i, j) + query(pr[p1], pr[p2], m + 1, r, i, j);
}

int update(int p, int l, int r, int i, const Node &cur) {
    if (l == r)
        return newLeaf(st[p] + cur);
    int m = (l + r) / 2;
    if (i <= m)
        return newParent(update(pl[p], l, m, i, cur), pr[p]);
    else
        return newParent(pl[p], update(pr[p], m + 1, r, i, cur));
}

int n, m, mnR, mxR, mnC, mxC, pseg[MAXC];
set<int> cells[MAXC];
map<int, Node> build[MAXC];

void init(int R, int C, int sr, int sc, int M, char *S) {
    n = R;
    m = C;
    st[0] = null;
    pl[0] = pr[0] = 0;
    id = 1;
    mnR = mxR = sr;
    mnC = mxC = sc;
    cells[sc].insert(sr);
    for (int i=0; i<M; i++) {
        if (S[i] == 'N') sr--;
        else if (S[i] == 'S') sr++;
        else if (S[i] == 'E') sc++;
        else sc--;
        mnR = min(mnR, sr);
        mxR = max(mxR, sr);
        mnC = min(mnC, sc);
        mxC = max(mxC, sc);
        cells[sc].insert(sr);
    }
    for (int c=1; c<=m; c++)
        for (int r : cells[c]) {
            // bottom right
            build[r+1][c+1].vertices++;
            // bottom left
            if (!cells[c-1].count(r) && !cells[c-1].count(r + 1))
                build[r+1][c].vertices++;
            // top right
            if (!cells[c].count(r - 1))
                build[r][c+1].vertices++;
            // top left
            if (!cells[c-1].count(r) && !cells[c-1].count(r - 1) && !cells[c].count(r - 1))
                build[r][c].vertices++;
            // bottom and right
            build[r+1][c].horiEdges++;
            build[r][c+1].vertEdges++;
            // left
            if (!cells[c-1].count(r))
                build[r][c].vertEdges++;
            // top
            if (!cells[c].count(r - 1))
                build[r][c].horiEdges++;
            build[r][c].river++;
        }
    for (int i=1; i<=n+1; i++) {
        pseg[i] = pseg[i-1];
        for (const auto &p : build[i])
            pseg[i] = update(pseg[i], 1, m + 1, p.first, p.second);
    }
}

int colour(int ar, int ac, int br, int bc) {
    int vertices = query(pseg[ar], pseg[br], 1, m + 1, ac + 1, bc).vertices;
    int edges = query(pseg[ar], pseg[br], 1, m + 1, ac, bc).horiEdges + query(pseg[ar-1], pseg[br], 1, m + 1, ac + 1, bc).vertEdges;
    int river = query(pseg[ar-1], pseg[br], 1, m + 1, ac, bc).river;
    int comp = mnR > ar && mxR < br && mnC > ac && mxC < bc ? 2 : 1;
    return edges + comp - vertices - river;
}

# Verdict Execution time Memory Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 11 ms 19540 KB Output is correct
3 Correct 11 ms 19240 KB Output is correct
4 Correct 11 ms 19156 KB Output is correct
5 Correct 12 ms 19540 KB Output is correct
6 Correct 10 ms 19100 KB Output is correct
7 Correct 10 ms 19092 KB Output is correct
8 Correct 9 ms 19092 KB Output is correct
9 Correct 10 ms 19028 KB Output is correct
10 Correct 10 ms 19028 KB Output is correct
11 Correct 13 ms 19284 KB Output is correct
12 Correct 12 ms 19412 KB Output is correct
13 Correct 14 ms 19744 KB Output is correct
14 Correct 14 ms 19744 KB Output is correct
15 Correct 10 ms 19024 KB Output is correct
16 Correct 10 ms 19080 KB Output is correct
17 Correct 10 ms 18996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19080 KB Output is correct
2 Correct 10 ms 18996 KB Output is correct
3 Correct 325 ms 85288 KB Output is correct
4 Correct 419 ms 127824 KB Output is correct
5 Correct 440 ms 126604 KB Output is correct
6 Correct 392 ms 99420 KB Output is correct
7 Correct 419 ms 101532 KB Output is correct
8 Correct 223 ms 22852 KB Output is correct
9 Correct 469 ms 127620 KB Output is correct
10 Correct 430 ms 126752 KB Output is correct
11 Correct 391 ms 99304 KB Output is correct
12 Correct 343 ms 120280 KB Output is correct
13 Correct 376 ms 127720 KB Output is correct
14 Correct 397 ms 126608 KB Output is correct
15 Correct 349 ms 99440 KB Output is correct
16 Correct 483 ms 96832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19024 KB Output is correct
2 Correct 208 ms 124892 KB Output is correct
3 Correct 145 ms 124028 KB Output is correct
4 Correct 108 ms 124872 KB Output is correct
5 Correct 122 ms 98280 KB Output is correct
6 Correct 51 ms 36580 KB Output is correct
7 Correct 85 ms 52796 KB Output is correct
8 Correct 157 ms 102152 KB Output is correct
9 Correct 146 ms 91116 KB Output is correct
10 Correct 50 ms 32664 KB Output is correct
11 Correct 80 ms 64576 KB Output is correct
12 Correct 193 ms 124964 KB Output is correct
13 Correct 149 ms 124108 KB Output is correct
14 Correct 140 ms 124872 KB Output is correct
15 Correct 119 ms 98252 KB Output is correct
16 Correct 38 ms 33000 KB Output is correct
17 Correct 75 ms 52840 KB Output is correct
18 Correct 178 ms 124876 KB Output is correct
19 Correct 154 ms 125680 KB Output is correct
20 Correct 155 ms 125512 KB Output is correct
21 Correct 150 ms 102224 KB Output is correct
22 Correct 124 ms 91084 KB Output is correct
23 Correct 35 ms 32656 KB Output is correct
24 Correct 73 ms 64560 KB Output is correct
25 Correct 220 ms 125012 KB Output is correct
26 Correct 126 ms 124048 KB Output is correct
27 Correct 98 ms 124872 KB Output is correct
28 Correct 105 ms 98216 KB Output is correct
29 Correct 37 ms 33108 KB Output is correct
30 Correct 91 ms 52908 KB Output is correct
31 Correct 164 ms 124956 KB Output is correct
32 Correct 152 ms 125632 KB Output is correct
33 Correct 150 ms 125560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 11 ms 19540 KB Output is correct
3 Correct 11 ms 19240 KB Output is correct
4 Correct 11 ms 19156 KB Output is correct
5 Correct 12 ms 19540 KB Output is correct
6 Correct 10 ms 19100 KB Output is correct
7 Correct 10 ms 19092 KB Output is correct
8 Correct 9 ms 19092 KB Output is correct
9 Correct 10 ms 19028 KB Output is correct
10 Correct 10 ms 19028 KB Output is correct
11 Correct 13 ms 19284 KB Output is correct
12 Correct 12 ms 19412 KB Output is correct
13 Correct 14 ms 19744 KB Output is correct
14 Correct 14 ms 19744 KB Output is correct
15 Correct 10 ms 19024 KB Output is correct
16 Correct 10 ms 19080 KB Output is correct
17 Correct 10 ms 18996 KB Output is correct
18 Correct 363 ms 57400 KB Output is correct
19 Correct 143 ms 24012 KB Output is correct
20 Correct 134 ms 22740 KB Output is correct
21 Correct 131 ms 23016 KB Output is correct
22 Correct 118 ms 23264 KB Output is correct
23 Correct 140 ms 24004 KB Output is correct
24 Correct 116 ms 22832 KB Output is correct
25 Correct 115 ms 23168 KB Output is correct
26 Correct 122 ms 23512 KB Output is correct
27 Correct 208 ms 48204 KB Output is correct
28 Correct 177 ms 34368 KB Output is correct
29 Correct 201 ms 44516 KB Output is correct
30 Correct 348 ms 91532 KB Output is correct
31 Correct 12 ms 19156 KB Output is correct
32 Correct 232 ms 46268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 11 ms 19540 KB Output is correct
3 Correct 11 ms 19240 KB Output is correct
4 Correct 11 ms 19156 KB Output is correct
5 Correct 12 ms 19540 KB Output is correct
6 Correct 10 ms 19100 KB Output is correct
7 Correct 10 ms 19092 KB Output is correct
8 Correct 9 ms 19092 KB Output is correct
9 Correct 10 ms 19028 KB Output is correct
10 Correct 10 ms 19028 KB Output is correct
11 Correct 13 ms 19284 KB Output is correct
12 Correct 12 ms 19412 KB Output is correct
13 Correct 14 ms 19744 KB Output is correct
14 Correct 14 ms 19744 KB Output is correct
15 Correct 10 ms 19024 KB Output is correct
16 Correct 10 ms 19080 KB Output is correct
17 Correct 10 ms 18996 KB Output is correct
18 Correct 363 ms 57400 KB Output is correct
19 Correct 143 ms 24012 KB Output is correct
20 Correct 134 ms 22740 KB Output is correct
21 Correct 131 ms 23016 KB Output is correct
22 Correct 118 ms 23264 KB Output is correct
23 Correct 140 ms 24004 KB Output is correct
24 Correct 116 ms 22832 KB Output is correct
25 Correct 115 ms 23168 KB Output is correct
26 Correct 122 ms 23512 KB Output is correct
27 Correct 208 ms 48204 KB Output is correct
28 Correct 177 ms 34368 KB Output is correct
29 Correct 201 ms 44516 KB Output is correct
30 Correct 348 ms 91532 KB Output is correct
31 Correct 12 ms 19156 KB Output is correct
32 Correct 232 ms 46268 KB Output is correct
33 Correct 208 ms 124892 KB Output is correct
34 Correct 145 ms 124028 KB Output is correct
35 Correct 108 ms 124872 KB Output is correct
36 Correct 122 ms 98280 KB Output is correct
37 Correct 51 ms 36580 KB Output is correct
38 Correct 85 ms 52796 KB Output is correct
39 Correct 157 ms 102152 KB Output is correct
40 Correct 146 ms 91116 KB Output is correct
41 Correct 50 ms 32664 KB Output is correct
42 Correct 80 ms 64576 KB Output is correct
43 Correct 193 ms 124964 KB Output is correct
44 Correct 149 ms 124108 KB Output is correct
45 Correct 140 ms 124872 KB Output is correct
46 Correct 119 ms 98252 KB Output is correct
47 Correct 38 ms 33000 KB Output is correct
48 Correct 75 ms 52840 KB Output is correct
49 Correct 178 ms 124876 KB Output is correct
50 Correct 154 ms 125680 KB Output is correct
51 Correct 155 ms 125512 KB Output is correct
52 Correct 150 ms 102224 KB Output is correct
53 Correct 124 ms 91084 KB Output is correct
54 Correct 35 ms 32656 KB Output is correct
55 Correct 73 ms 64560 KB Output is correct
56 Correct 220 ms 125012 KB Output is correct
57 Correct 126 ms 124048 KB Output is correct
58 Correct 98 ms 124872 KB Output is correct
59 Correct 105 ms 98216 KB Output is correct
60 Correct 37 ms 33108 KB Output is correct
61 Correct 91 ms 52908 KB Output is correct
62 Correct 164 ms 124956 KB Output is correct
63 Correct 152 ms 125632 KB Output is correct
64 Correct 150 ms 125560 KB Output is correct
65 Correct 325 ms 85288 KB Output is correct
66 Correct 419 ms 127824 KB Output is correct
67 Correct 440 ms 126604 KB Output is correct
68 Correct 392 ms 99420 KB Output is correct
69 Correct 419 ms 101532 KB Output is correct
70 Correct 223 ms 22852 KB Output is correct
71 Correct 469 ms 127620 KB Output is correct
72 Correct 430 ms 126752 KB Output is correct
73 Correct 391 ms 99304 KB Output is correct
74 Correct 343 ms 120280 KB Output is correct
75 Correct 376 ms 127720 KB Output is correct
76 Correct 397 ms 126608 KB Output is correct
77 Correct 349 ms 99440 KB Output is correct
78 Correct 483 ms 96832 KB Output is correct
79 Correct 508 ms 105792 KB Output is correct
80 Correct 407 ms 94576 KB Output is correct
81 Correct 209 ms 36316 KB Output is correct
82 Correct 260 ms 68112 KB Output is correct
83 Correct 427 ms 128596 KB Output is correct
84 Correct 330 ms 127684 KB Output is correct
85 Correct 373 ms 128604 KB Output is correct
86 Correct 307 ms 101832 KB Output is correct
87 Correct 258 ms 36680 KB Output is correct
88 Correct 258 ms 56648 KB Output is correct
89 Correct 353 ms 128608 KB Output is correct
90 Correct 418 ms 129216 KB Output is correct
91 Correct 357 ms 129108 KB Output is correct