Submission #49582

# Submission time Handle Problem Language Result Execution time Memory
49582 2018-05-31T14:22:10 Z model_code Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
1630 ms 233976 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 45 ms 26176 KB Output is correct
2 Correct 47 ms 27184 KB Output is correct
3 Correct 44 ms 27184 KB Output is correct
4 Correct 46 ms 27184 KB Output is correct
5 Correct 48 ms 27416 KB Output is correct
6 Correct 47 ms 27416 KB Output is correct
7 Correct 41 ms 27416 KB Output is correct
8 Correct 44 ms 27416 KB Output is correct
9 Correct 43 ms 27416 KB Output is correct
10 Correct 41 ms 27416 KB Output is correct
11 Correct 44 ms 27416 KB Output is correct
12 Correct 48 ms 27416 KB Output is correct
13 Correct 49 ms 27988 KB Output is correct
14 Correct 52 ms 28352 KB Output is correct
15 Correct 46 ms 28352 KB Output is correct
16 Correct 48 ms 28352 KB Output is correct
17 Correct 49 ms 28352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 28352 KB Output is correct
2 Correct 49 ms 28352 KB Output is correct
3 Correct 1239 ms 143432 KB Output is correct
4 Correct 1459 ms 228420 KB Output is correct
5 Correct 1630 ms 228420 KB Output is correct
6 Correct 1290 ms 228420 KB Output is correct
7 Correct 1560 ms 228420 KB Output is correct
8 Correct 739 ms 228420 KB Output is correct
9 Correct 1477 ms 228420 KB Output is correct
10 Correct 1588 ms 228420 KB Output is correct
11 Correct 1320 ms 228420 KB Output is correct
12 Correct 937 ms 228420 KB Output is correct
13 Correct 1078 ms 228516 KB Output is correct
14 Correct 993 ms 228516 KB Output is correct
15 Correct 881 ms 228516 KB Output is correct
16 Correct 1250 ms 228516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 28352 KB Output is correct
2 Correct 763 ms 229332 KB Output is correct
3 Correct 736 ms 233976 KB Output is correct
4 Correct 673 ms 233976 KB Output is correct
5 Correct 547 ms 233976 KB Output is correct
6 Correct 149 ms 233976 KB Output is correct
7 Correct 284 ms 233976 KB Output is correct
8 Correct 701 ms 233976 KB Output is correct
9 Correct 552 ms 233976 KB Output is correct
10 Correct 188 ms 233976 KB Output is correct
11 Correct 399 ms 233976 KB Output is correct
12 Correct 730 ms 233976 KB Output is correct
13 Correct 813 ms 233976 KB Output is correct
14 Correct 685 ms 233976 KB Output is correct
15 Correct 518 ms 233976 KB Output is correct
16 Correct 136 ms 233976 KB Output is correct
17 Correct 275 ms 233976 KB Output is correct
18 Correct 631 ms 233976 KB Output is correct
19 Correct 715 ms 233976 KB Output is correct
20 Correct 686 ms 233976 KB Output is correct
21 Correct 628 ms 233976 KB Output is correct
22 Correct 588 ms 233976 KB Output is correct
23 Correct 155 ms 233976 KB Output is correct
24 Correct 339 ms 233976 KB Output is correct
25 Correct 734 ms 233976 KB Output is correct
26 Correct 769 ms 233976 KB Output is correct
27 Correct 757 ms 233976 KB Output is correct
28 Correct 522 ms 233976 KB Output is correct
29 Correct 142 ms 233976 KB Output is correct
30 Correct 298 ms 233976 KB Output is correct
31 Correct 627 ms 233976 KB Output is correct
32 Correct 754 ms 233976 KB Output is correct
33 Correct 662 ms 233976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 26176 KB Output is correct
2 Correct 47 ms 27184 KB Output is correct
3 Correct 44 ms 27184 KB Output is correct
4 Correct 46 ms 27184 KB Output is correct
5 Correct 48 ms 27416 KB Output is correct
6 Correct 47 ms 27416 KB Output is correct
7 Correct 41 ms 27416 KB Output is correct
8 Correct 44 ms 27416 KB Output is correct
9 Correct 43 ms 27416 KB Output is correct
10 Correct 41 ms 27416 KB Output is correct
11 Correct 44 ms 27416 KB Output is correct
12 Correct 48 ms 27416 KB Output is correct
13 Correct 49 ms 27988 KB Output is correct
14 Correct 52 ms 28352 KB Output is correct
15 Correct 46 ms 28352 KB Output is correct
16 Correct 48 ms 28352 KB Output is correct
17 Correct 49 ms 28352 KB Output is correct
18 Correct 1189 ms 233976 KB Output is correct
19 Correct 361 ms 233976 KB Output is correct
20 Correct 272 ms 233976 KB Output is correct
21 Correct 275 ms 233976 KB Output is correct
22 Correct 296 ms 233976 KB Output is correct
23 Correct 352 ms 233976 KB Output is correct
24 Correct 256 ms 233976 KB Output is correct
25 Correct 283 ms 233976 KB Output is correct
26 Correct 309 ms 233976 KB Output is correct
27 Correct 660 ms 233976 KB Output is correct
28 Correct 435 ms 233976 KB Output is correct
29 Correct 604 ms 233976 KB Output is correct
30 Correct 1073 ms 233976 KB Output is correct
31 Correct 47 ms 233976 KB Output is correct
32 Correct 1062 ms 233976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 26176 KB Output is correct
2 Correct 47 ms 27184 KB Output is correct
3 Correct 44 ms 27184 KB Output is correct
4 Correct 46 ms 27184 KB Output is correct
5 Correct 48 ms 27416 KB Output is correct
6 Correct 47 ms 27416 KB Output is correct
7 Correct 41 ms 27416 KB Output is correct
8 Correct 44 ms 27416 KB Output is correct
9 Correct 43 ms 27416 KB Output is correct
10 Correct 41 ms 27416 KB Output is correct
11 Correct 44 ms 27416 KB Output is correct
12 Correct 48 ms 27416 KB Output is correct
13 Correct 49 ms 27988 KB Output is correct
14 Correct 52 ms 28352 KB Output is correct
15 Correct 46 ms 28352 KB Output is correct
16 Correct 48 ms 28352 KB Output is correct
17 Correct 49 ms 28352 KB Output is correct
18 Correct 1189 ms 233976 KB Output is correct
19 Correct 361 ms 233976 KB Output is correct
20 Correct 272 ms 233976 KB Output is correct
21 Correct 275 ms 233976 KB Output is correct
22 Correct 296 ms 233976 KB Output is correct
23 Correct 352 ms 233976 KB Output is correct
24 Correct 256 ms 233976 KB Output is correct
25 Correct 283 ms 233976 KB Output is correct
26 Correct 309 ms 233976 KB Output is correct
27 Correct 660 ms 233976 KB Output is correct
28 Correct 435 ms 233976 KB Output is correct
29 Correct 604 ms 233976 KB Output is correct
30 Correct 1073 ms 233976 KB Output is correct
31 Correct 47 ms 233976 KB Output is correct
32 Correct 1062 ms 233976 KB Output is correct
33 Correct 763 ms 229332 KB Output is correct
34 Correct 736 ms 233976 KB Output is correct
35 Correct 673 ms 233976 KB Output is correct
36 Correct 547 ms 233976 KB Output is correct
37 Correct 149 ms 233976 KB Output is correct
38 Correct 284 ms 233976 KB Output is correct
39 Correct 701 ms 233976 KB Output is correct
40 Correct 552 ms 233976 KB Output is correct
41 Correct 188 ms 233976 KB Output is correct
42 Correct 399 ms 233976 KB Output is correct
43 Correct 730 ms 233976 KB Output is correct
44 Correct 813 ms 233976 KB Output is correct
45 Correct 685 ms 233976 KB Output is correct
46 Correct 518 ms 233976 KB Output is correct
47 Correct 136 ms 233976 KB Output is correct
48 Correct 275 ms 233976 KB Output is correct
49 Correct 631 ms 233976 KB Output is correct
50 Correct 715 ms 233976 KB Output is correct
51 Correct 686 ms 233976 KB Output is correct
52 Correct 628 ms 233976 KB Output is correct
53 Correct 588 ms 233976 KB Output is correct
54 Correct 155 ms 233976 KB Output is correct
55 Correct 339 ms 233976 KB Output is correct
56 Correct 734 ms 233976 KB Output is correct
57 Correct 769 ms 233976 KB Output is correct
58 Correct 757 ms 233976 KB Output is correct
59 Correct 522 ms 233976 KB Output is correct
60 Correct 142 ms 233976 KB Output is correct
61 Correct 298 ms 233976 KB Output is correct
62 Correct 627 ms 233976 KB Output is correct
63 Correct 754 ms 233976 KB Output is correct
64 Correct 662 ms 233976 KB Output is correct
65 Correct 1566 ms 233976 KB Output is correct
66 Correct 1583 ms 233976 KB Output is correct
67 Correct 643 ms 233976 KB Output is correct
68 Correct 986 ms 233976 KB Output is correct
69 Correct 1526 ms 233976 KB Output is correct
70 Correct 1512 ms 233976 KB Output is correct
71 Correct 1356 ms 233976 KB Output is correct
72 Correct 1281 ms 233976 KB Output is correct
73 Correct 862 ms 233976 KB Output is correct
74 Correct 974 ms 233976 KB Output is correct
75 Correct 1375 ms 233976 KB Output is correct
76 Correct 1518 ms 233976 KB Output is correct
77 Correct 1375 ms 233976 KB Output is correct
78 Correct 1239 ms 143432 KB Output is correct
79 Correct 1459 ms 228420 KB Output is correct
80 Correct 1630 ms 228420 KB Output is correct
81 Correct 1290 ms 228420 KB Output is correct
82 Correct 1560 ms 228420 KB Output is correct
83 Correct 739 ms 228420 KB Output is correct
84 Correct 1477 ms 228420 KB Output is correct
85 Correct 1588 ms 228420 KB Output is correct
86 Correct 1320 ms 228420 KB Output is correct
87 Correct 937 ms 228420 KB Output is correct
88 Correct 1078 ms 228516 KB Output is correct
89 Correct 993 ms 228516 KB Output is correct
90 Correct 881 ms 228516 KB Output is correct
91 Correct 1250 ms 228516 KB Output is correct