Submission #56545

# Submission time Handle Problem Language Result Execution time Memory
56545 2018-07-11T15:19:23 Z andremfq Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
1862 ms 336480 KB
#include <bits/stdc++.h>

#define sz(x) (int((x).size()))

struct RT {
    struct node {
        int sum, left, right;
        node() : sum(0), left(0), right(0) {}
        node(const node &n) : sum(n.sum), left(n.left), right(n.right) {}
        node(int s, int l, int r) : sum(s), left(l), right(r) {}
    };

    std::vector<node> nodes;

    int newNode(node n = node()) {
        nodes.push_back(n);
        return sz(nodes) - 1;
    }

    int init(int nl, int nr) {
        int n = newNode();
        int nm = (nl+nr)/2;
        if (nl+1 < nr) {
            int l = init(nl, nm);
            nodes[n].left = l;
            int r = init(nm, nr);
            nodes[n].right = r;
        }
        return n;
    }

    int update(int old, int nl, int nr, int ui) {
        int now = newNode(nodes[old]);
        if (nl == ui && nl + 1 == nr) {
            nodes[now].sum++;
        } else {
            int nm = (nl+nr)/2;
            if (ui < nm) {
                int l = update(nodes[old].left, nl, nm, ui);
                nodes[now].left = l;
            } else {
                int r = update(nodes[old].right, nm, nr, ui);
                nodes[now].right = r;
            }
            nodes[now].sum = nodes[nodes[now].left].sum + nodes[nodes[now].right].sum;
        }
        return now;
    }

    int query(int now, int nl, int nr, int ql, int qr) {
        if (ql <= nl && nr <= qr) {
            return nodes[now].sum;
        } else {
            int sum = 0;
            int nm = (nl+nr)/2;
            if (ql < nm) sum += query(nodes[now].left, nl, nm, ql, qr);
            if (nm < qr) sum += query(nodes[now].right, nm, nr, ql, qr);
            return sum;
        }
    }
};

const int MaxC = 200*1000+5;

std::set<std::pair<int, int>> squares, vertices, hEdges, vEdges;
// these range trees are "horizontal" range trees over columns,
// sweeping vertically over rows over time
RT rtSquares, rtVertices, rtHEdges, rtVEdges;
int rootsSquares[MaxC], rootsVertices[MaxC], rootsHEdges[MaxC], rootsVEdges[MaxC];
int minr, maxr, minc, maxc;

void setupRT(RT &rt, int *roots, std::set<std::pair<int, int>> &points) {
    roots[0] = rt.init(0, MaxC);
    auto it = points.begin();
    for (int i = 0; i < MaxC; i++) {
        if (i) roots[i] = roots[i-1];
        while (it != points.end() && it->first == i) {
            roots[i] = rt.update(roots[i], 0, MaxC, it->second);
            it++;
        }
    }
}

void init(int R, int C, int sr, int sc, int M, char *S) {
    squares.emplace(sr, sc);
    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 /* S[i] == 'W' */ sc--;
        squares.emplace(sr, sc);
    }

    minr = minc = MaxC + 5;
    maxr = maxc = -5;

    for (auto &square : squares) {
        int r, c; std::tie(r, c) = square;

        minr = std::min(minr, r);
        minc = std::min(minc, c);
        maxr = std::max(maxr, r);
        maxc = std::max(maxc, c);

        vertices.emplace(r, c);
        vertices.emplace(r+1, c);
        vertices.emplace(r, c+1);
        vertices.emplace(r+1, c+1);

        hEdges.emplace(r, c);
        hEdges.emplace(r+1, c);
        vEdges.emplace(r, c);
        vEdges.emplace(r, c+1);
    }

    setupRT(rtSquares, rootsSquares, squares);
    setupRT(rtVertices, rootsVertices, vertices);
    setupRT(rtHEdges, rootsHEdges, hEdges);
    setupRT(rtVEdges, rootsVEdges, vEdges);
}

// both coordinates are [inclusive, inclusive]
int queryRT(RT &rt, int *roots, int ay, int by, int ax, int bx) {
    assert(ax-1 <= bx && ay <= by + 1);
    return rt.query(roots[by], 0, MaxC, ax, bx + 1) -
           rt.query(roots[ay-1], 0, MaxC, ax, bx + 1);
}

int colour(int ar, int ac, int br, int bc) {
    int V = /* internal vertices */
            queryRT(rtVertices, rootsVertices, ar+1, br, ac+1, bc) +
            /* external vertices */
            4 + 2 * (br - ar + bc - ac);
    int E = /* internal edges */
            queryRT(rtHEdges, rootsHEdges, ar+1, br, ac, bc) +
            queryRT(rtVEdges, rootsVEdges, ar, br, ac+1, bc) +
            /* external edges */
            2 * (br - ar + 1 + bc - ac + 1);
    // black faces
    int BF = /* internal faces */
             queryRT(rtSquares, rootsSquares, ar, br, ac, bc) +
             /* the infinite, external face */
             1;
    // components (because the outer black component might not be
    // connected to the original black component)
    int C = ((ac < minc && maxc < bc && ar < minr && maxr < br) ? 2 : 1);
    // white faces
    int WF = 1 + C - V + E - BF;
    return WF;
}

# Verdict Execution time Memory Grader output
1 Correct 57 ms 26336 KB Output is correct
2 Correct 65 ms 27176 KB Output is correct
3 Correct 59 ms 27176 KB Output is correct
4 Correct 58 ms 27176 KB Output is correct
5 Correct 59 ms 27380 KB Output is correct
6 Correct 50 ms 27380 KB Output is correct
7 Correct 59 ms 27380 KB Output is correct
8 Correct 49 ms 27380 KB Output is correct
9 Correct 52 ms 27380 KB Output is correct
10 Correct 49 ms 27380 KB Output is correct
11 Correct 59 ms 27380 KB Output is correct
12 Correct 57 ms 27380 KB Output is correct
13 Correct 60 ms 28248 KB Output is correct
14 Correct 60 ms 28588 KB Output is correct
15 Correct 59 ms 28588 KB Output is correct
16 Correct 65 ms 28588 KB Output is correct
17 Correct 64 ms 28588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 28588 KB Output is correct
2 Correct 64 ms 28588 KB Output is correct
3 Correct 1501 ms 146524 KB Output is correct
4 Correct 1862 ms 232160 KB Output is correct
5 Correct 1812 ms 232272 KB Output is correct
6 Correct 1658 ms 232272 KB Output is correct
7 Correct 1615 ms 232272 KB Output is correct
8 Correct 882 ms 232272 KB Output is correct
9 Correct 1819 ms 246516 KB Output is correct
10 Correct 1849 ms 246692 KB Output is correct
11 Correct 1644 ms 246692 KB Output is correct
12 Correct 1144 ms 246692 KB Output is correct
13 Correct 1108 ms 258068 KB Output is correct
14 Correct 1229 ms 258140 KB Output is correct
15 Correct 1018 ms 258140 KB Output is correct
16 Correct 1597 ms 258140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 28588 KB Output is correct
2 Correct 809 ms 270284 KB Output is correct
3 Correct 783 ms 274588 KB Output is correct
4 Correct 793 ms 274588 KB Output is correct
5 Correct 645 ms 274588 KB Output is correct
6 Correct 179 ms 274588 KB Output is correct
7 Correct 329 ms 274588 KB Output is correct
8 Correct 688 ms 274588 KB Output is correct
9 Correct 685 ms 274588 KB Output is correct
10 Correct 181 ms 274588 KB Output is correct
11 Correct 457 ms 274588 KB Output is correct
12 Correct 801 ms 274588 KB Output is correct
13 Correct 848 ms 275528 KB Output is correct
14 Correct 804 ms 275528 KB Output is correct
15 Correct 716 ms 275528 KB Output is correct
16 Correct 213 ms 275528 KB Output is correct
17 Correct 346 ms 275528 KB Output is correct
18 Correct 753 ms 275528 KB Output is correct
19 Correct 774 ms 275528 KB Output is correct
20 Correct 798 ms 275528 KB Output is correct
21 Correct 719 ms 275528 KB Output is correct
22 Correct 648 ms 275528 KB Output is correct
23 Correct 189 ms 275528 KB Output is correct
24 Correct 388 ms 275528 KB Output is correct
25 Correct 744 ms 275528 KB Output is correct
26 Correct 845 ms 276972 KB Output is correct
27 Correct 762 ms 276972 KB Output is correct
28 Correct 560 ms 276972 KB Output is correct
29 Correct 142 ms 276972 KB Output is correct
30 Correct 279 ms 276972 KB Output is correct
31 Correct 647 ms 276972 KB Output is correct
32 Correct 741 ms 276972 KB Output is correct
33 Correct 739 ms 276972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 26336 KB Output is correct
2 Correct 65 ms 27176 KB Output is correct
3 Correct 59 ms 27176 KB Output is correct
4 Correct 58 ms 27176 KB Output is correct
5 Correct 59 ms 27380 KB Output is correct
6 Correct 50 ms 27380 KB Output is correct
7 Correct 59 ms 27380 KB Output is correct
8 Correct 49 ms 27380 KB Output is correct
9 Correct 52 ms 27380 KB Output is correct
10 Correct 49 ms 27380 KB Output is correct
11 Correct 59 ms 27380 KB Output is correct
12 Correct 57 ms 27380 KB Output is correct
13 Correct 60 ms 28248 KB Output is correct
14 Correct 60 ms 28588 KB Output is correct
15 Correct 59 ms 28588 KB Output is correct
16 Correct 65 ms 28588 KB Output is correct
17 Correct 64 ms 28588 KB Output is correct
18 Correct 1394 ms 276972 KB Output is correct
19 Correct 412 ms 276972 KB Output is correct
20 Correct 292 ms 276972 KB Output is correct
21 Correct 314 ms 276972 KB Output is correct
22 Correct 373 ms 276972 KB Output is correct
23 Correct 367 ms 276972 KB Output is correct
24 Correct 290 ms 276972 KB Output is correct
25 Correct 299 ms 276972 KB Output is correct
26 Correct 409 ms 276972 KB Output is correct
27 Correct 858 ms 276972 KB Output is correct
28 Correct 636 ms 276972 KB Output is correct
29 Correct 716 ms 276972 KB Output is correct
30 Correct 1260 ms 295232 KB Output is correct
31 Correct 62 ms 295232 KB Output is correct
32 Correct 982 ms 295232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 26336 KB Output is correct
2 Correct 65 ms 27176 KB Output is correct
3 Correct 59 ms 27176 KB Output is correct
4 Correct 58 ms 27176 KB Output is correct
5 Correct 59 ms 27380 KB Output is correct
6 Correct 50 ms 27380 KB Output is correct
7 Correct 59 ms 27380 KB Output is correct
8 Correct 49 ms 27380 KB Output is correct
9 Correct 52 ms 27380 KB Output is correct
10 Correct 49 ms 27380 KB Output is correct
11 Correct 59 ms 27380 KB Output is correct
12 Correct 57 ms 27380 KB Output is correct
13 Correct 60 ms 28248 KB Output is correct
14 Correct 60 ms 28588 KB Output is correct
15 Correct 59 ms 28588 KB Output is correct
16 Correct 65 ms 28588 KB Output is correct
17 Correct 64 ms 28588 KB Output is correct
18 Correct 1394 ms 276972 KB Output is correct
19 Correct 412 ms 276972 KB Output is correct
20 Correct 292 ms 276972 KB Output is correct
21 Correct 314 ms 276972 KB Output is correct
22 Correct 373 ms 276972 KB Output is correct
23 Correct 367 ms 276972 KB Output is correct
24 Correct 290 ms 276972 KB Output is correct
25 Correct 299 ms 276972 KB Output is correct
26 Correct 409 ms 276972 KB Output is correct
27 Correct 858 ms 276972 KB Output is correct
28 Correct 636 ms 276972 KB Output is correct
29 Correct 716 ms 276972 KB Output is correct
30 Correct 1260 ms 295232 KB Output is correct
31 Correct 62 ms 295232 KB Output is correct
32 Correct 982 ms 295232 KB Output is correct
33 Correct 809 ms 270284 KB Output is correct
34 Correct 783 ms 274588 KB Output is correct
35 Correct 793 ms 274588 KB Output is correct
36 Correct 645 ms 274588 KB Output is correct
37 Correct 179 ms 274588 KB Output is correct
38 Correct 329 ms 274588 KB Output is correct
39 Correct 688 ms 274588 KB Output is correct
40 Correct 685 ms 274588 KB Output is correct
41 Correct 181 ms 274588 KB Output is correct
42 Correct 457 ms 274588 KB Output is correct
43 Correct 801 ms 274588 KB Output is correct
44 Correct 848 ms 275528 KB Output is correct
45 Correct 804 ms 275528 KB Output is correct
46 Correct 716 ms 275528 KB Output is correct
47 Correct 213 ms 275528 KB Output is correct
48 Correct 346 ms 275528 KB Output is correct
49 Correct 753 ms 275528 KB Output is correct
50 Correct 774 ms 275528 KB Output is correct
51 Correct 798 ms 275528 KB Output is correct
52 Correct 719 ms 275528 KB Output is correct
53 Correct 648 ms 275528 KB Output is correct
54 Correct 189 ms 275528 KB Output is correct
55 Correct 388 ms 275528 KB Output is correct
56 Correct 744 ms 275528 KB Output is correct
57 Correct 845 ms 276972 KB Output is correct
58 Correct 762 ms 276972 KB Output is correct
59 Correct 560 ms 276972 KB Output is correct
60 Correct 142 ms 276972 KB Output is correct
61 Correct 279 ms 276972 KB Output is correct
62 Correct 647 ms 276972 KB Output is correct
63 Correct 741 ms 276972 KB Output is correct
64 Correct 739 ms 276972 KB Output is correct
65 Correct 1755 ms 295232 KB Output is correct
66 Correct 1748 ms 295232 KB Output is correct
67 Correct 748 ms 295232 KB Output is correct
68 Correct 1125 ms 295232 KB Output is correct
69 Correct 1819 ms 323820 KB Output is correct
70 Correct 1646 ms 331180 KB Output is correct
71 Correct 1587 ms 331180 KB Output is correct
72 Correct 1397 ms 331180 KB Output is correct
73 Correct 876 ms 331180 KB Output is correct
74 Correct 1006 ms 331180 KB Output is correct
75 Correct 1418 ms 331180 KB Output is correct
76 Correct 1704 ms 333632 KB Output is correct
77 Correct 1430 ms 336480 KB Output is correct
78 Correct 1501 ms 146524 KB Output is correct
79 Correct 1862 ms 232160 KB Output is correct
80 Correct 1812 ms 232272 KB Output is correct
81 Correct 1658 ms 232272 KB Output is correct
82 Correct 1615 ms 232272 KB Output is correct
83 Correct 882 ms 232272 KB Output is correct
84 Correct 1819 ms 246516 KB Output is correct
85 Correct 1849 ms 246692 KB Output is correct
86 Correct 1644 ms 246692 KB Output is correct
87 Correct 1144 ms 246692 KB Output is correct
88 Correct 1108 ms 258068 KB Output is correct
89 Correct 1229 ms 258140 KB Output is correct
90 Correct 1018 ms 258140 KB Output is correct
91 Correct 1597 ms 258140 KB Output is correct