Submission #820121

# Submission time Handle Problem Language Result Execution time Memory
820121 2023-08-10T19:47:06 Z Ahmed57 Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
993 ms 205548 KB
#include "rainbow.h"
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC target("sse4,avx2,fma,avx")
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;

const int MAXN = 2e5, MAXSEG = (6e5 + 9) * 19 + 1;

int cnt = 1, segtree[MAXSEG], left_c[MAXSEG], right_c[MAXSEG];

struct Segtree {
    set<int> data[MAXN + 1];
    int roots[MAXN + 2];

    void add(int x, int y) { data[x].insert(y); }

    void build() {
        FOR(i, 1, MAXN + 1) {
            roots[i + 1] = roots[i];
            for (int j : data[i]) update(j, roots[i + 1]);
        }
    }

    void update(int pos, int &node, int l = 1, int r = MAXN) {
        segtree[cnt] = segtree[node] + 1;
        left_c[cnt] = left_c[node];
        right_c[cnt] = right_c[node];
        node = cnt++;

        if (l == r) return;
        int mid = (l + r) / 2;
        if (pos > mid) update(pos, right_c[node], mid + 1, r);
        else update(pos, left_c[node], l, mid);
    }

    int query(int l1, int r1, int l2, int r2) {
        if (l2 > r2) return 0;
        return query(l2, r2, roots[r1 + 1], 1, MAXN) - query(l2, r2, roots[l1], 1, MAXN);
    }
    int query(int a, int b, int node, int l, int r) {
        if (a > r || b < l) return 0;
        if (a <= l && b >= r) return segtree[node];
        int mid = (l + r) / 2;
        return query(a, b, left_c[node], l, mid) + query(a, b, right_c[node], mid + 1, r);
    }
} vertices, edges_horiz, edges_vert, rivers;

int mx_r, mn_r, mx_c, mn_c;
 
void add_river(int x, int y) {
    vertices.add(x, y);
    vertices.add(x + 1, y);
    vertices.add(x, y + 1);
    vertices.add(x + 1, y + 1);
    edges_horiz.add(x, y);
    edges_horiz.add(x + 1, y);
    edges_vert.add(x, y);
    edges_vert.add(x, y + 1);
    rivers.add(x, y);
}
 
void init(int R, int C, int sr, int sc, int M, char *S) {
    add_river(sr, sc);
    mx_r = mn_r = sr;
    mx_c = mn_c = sc;
    FOR(i, 0, M) {
        if (S[i] == 'N') sr--;
        if (S[i] == 'E') sc++;
        if (S[i] == 'S') sr++;
        if (S[i] == 'W') sc--;
        add_river(sr, sc);
        mx_r = max(mx_r, sr);
        mn_r = min(mn_r, sr);
        mx_c = max(mx_c, sc);
        mn_c = min(mn_c, sc);
    }
    vertices.build();
    edges_horiz.build();
    edges_vert.build();
    rivers.build();
}
 
int colour(int ar, int ac, int br, int bc) {
    int E = edges_horiz.query(ar + 1, br, ac, bc) + edges_vert.query(ar, br, ac + 1, bc);
    int V = vertices.query(ar + 1, br, ac + 1, bc);
    int R = rivers.query(ar, br, ac, bc);
    int C = (ar >= mn_r || br <= mx_r || ac >= mn_c || bc <= mx_c ? 1 : 2);
    return E - V + C - R;
}
# Verdict Execution time Memory Grader output
1 Correct 22 ms 41244 KB Output is correct
2 Correct 28 ms 42364 KB Output is correct
3 Correct 24 ms 41344 KB Output is correct
4 Correct 22 ms 41428 KB Output is correct
5 Correct 25 ms 42568 KB Output is correct
6 Correct 23 ms 40972 KB Output is correct
7 Correct 21 ms 40916 KB Output is correct
8 Correct 21 ms 41036 KB Output is correct
9 Correct 21 ms 41024 KB Output is correct
10 Correct 23 ms 40984 KB Output is correct
11 Correct 23 ms 41576 KB Output is correct
12 Correct 25 ms 42012 KB Output is correct
13 Correct 24 ms 43096 KB Output is correct
14 Correct 25 ms 43556 KB Output is correct
15 Correct 21 ms 40916 KB Output is correct
16 Correct 21 ms 40956 KB Output is correct
17 Correct 21 ms 40916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 40956 KB Output is correct
2 Correct 21 ms 40916 KB Output is correct
3 Correct 514 ms 139848 KB Output is correct
4 Correct 663 ms 204392 KB Output is correct
5 Correct 715 ms 203076 KB Output is correct
6 Correct 576 ms 166720 KB Output is correct
7 Correct 675 ms 164476 KB Output is correct
8 Correct 242 ms 44600 KB Output is correct
9 Correct 662 ms 204376 KB Output is correct
10 Correct 713 ms 203080 KB Output is correct
11 Correct 587 ms 166596 KB Output is correct
12 Correct 381 ms 193160 KB Output is correct
13 Correct 343 ms 204260 KB Output is correct
14 Correct 358 ms 203004 KB Output is correct
15 Correct 346 ms 166768 KB Output is correct
16 Correct 585 ms 157464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 40916 KB Output is correct
2 Correct 305 ms 200872 KB Output is correct
3 Correct 135 ms 198472 KB Output is correct
4 Correct 170 ms 200996 KB Output is correct
5 Correct 155 ms 159712 KB Output is correct
6 Correct 75 ms 71116 KB Output is correct
7 Correct 128 ms 100852 KB Output is correct
8 Correct 264 ms 183824 KB Output is correct
9 Correct 260 ms 167712 KB Output is correct
10 Correct 85 ms 72632 KB Output is correct
11 Correct 150 ms 119404 KB Output is correct
12 Correct 308 ms 201044 KB Output is correct
13 Correct 143 ms 198472 KB Output is correct
14 Correct 189 ms 201012 KB Output is correct
15 Correct 160 ms 159800 KB Output is correct
16 Correct 78 ms 65084 KB Output is correct
17 Correct 130 ms 100864 KB Output is correct
18 Correct 236 ms 201040 KB Output is correct
19 Correct 236 ms 202008 KB Output is correct
20 Correct 218 ms 202012 KB Output is correct
21 Correct 257 ms 183700 KB Output is correct
22 Correct 237 ms 167728 KB Output is correct
23 Correct 83 ms 72648 KB Output is correct
24 Correct 159 ms 119468 KB Output is correct
25 Correct 300 ms 201056 KB Output is correct
26 Correct 139 ms 198544 KB Output is correct
27 Correct 168 ms 201048 KB Output is correct
28 Correct 156 ms 159768 KB Output is correct
29 Correct 70 ms 65044 KB Output is correct
30 Correct 129 ms 100876 KB Output is correct
31 Correct 241 ms 201068 KB Output is correct
32 Correct 214 ms 202108 KB Output is correct
33 Correct 218 ms 201904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 41244 KB Output is correct
2 Correct 28 ms 42364 KB Output is correct
3 Correct 24 ms 41344 KB Output is correct
4 Correct 22 ms 41428 KB Output is correct
5 Correct 25 ms 42568 KB Output is correct
6 Correct 23 ms 40972 KB Output is correct
7 Correct 21 ms 40916 KB Output is correct
8 Correct 21 ms 41036 KB Output is correct
9 Correct 21 ms 41024 KB Output is correct
10 Correct 23 ms 40984 KB Output is correct
11 Correct 23 ms 41576 KB Output is correct
12 Correct 25 ms 42012 KB Output is correct
13 Correct 24 ms 43096 KB Output is correct
14 Correct 25 ms 43556 KB Output is correct
15 Correct 21 ms 40916 KB Output is correct
16 Correct 21 ms 40956 KB Output is correct
17 Correct 21 ms 40916 KB Output is correct
18 Correct 734 ms 125236 KB Output is correct
19 Correct 219 ms 48048 KB Output is correct
20 Correct 178 ms 45052 KB Output is correct
21 Correct 168 ms 45592 KB Output is correct
22 Correct 177 ms 46412 KB Output is correct
23 Correct 203 ms 48120 KB Output is correct
24 Correct 163 ms 45464 KB Output is correct
25 Correct 166 ms 46156 KB Output is correct
26 Correct 174 ms 46712 KB Output is correct
27 Correct 344 ms 109772 KB Output is correct
28 Correct 260 ms 75924 KB Output is correct
29 Correct 355 ms 104104 KB Output is correct
30 Correct 528 ms 204500 KB Output is correct
31 Correct 24 ms 41116 KB Output is correct
32 Correct 505 ms 110588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 41244 KB Output is correct
2 Correct 28 ms 42364 KB Output is correct
3 Correct 24 ms 41344 KB Output is correct
4 Correct 22 ms 41428 KB Output is correct
5 Correct 25 ms 42568 KB Output is correct
6 Correct 23 ms 40972 KB Output is correct
7 Correct 21 ms 40916 KB Output is correct
8 Correct 21 ms 41036 KB Output is correct
9 Correct 21 ms 41024 KB Output is correct
10 Correct 23 ms 40984 KB Output is correct
11 Correct 23 ms 41576 KB Output is correct
12 Correct 25 ms 42012 KB Output is correct
13 Correct 24 ms 43096 KB Output is correct
14 Correct 25 ms 43556 KB Output is correct
15 Correct 21 ms 40916 KB Output is correct
16 Correct 21 ms 40956 KB Output is correct
17 Correct 21 ms 40916 KB Output is correct
18 Correct 734 ms 125236 KB Output is correct
19 Correct 219 ms 48048 KB Output is correct
20 Correct 178 ms 45052 KB Output is correct
21 Correct 168 ms 45592 KB Output is correct
22 Correct 177 ms 46412 KB Output is correct
23 Correct 203 ms 48120 KB Output is correct
24 Correct 163 ms 45464 KB Output is correct
25 Correct 166 ms 46156 KB Output is correct
26 Correct 174 ms 46712 KB Output is correct
27 Correct 344 ms 109772 KB Output is correct
28 Correct 260 ms 75924 KB Output is correct
29 Correct 355 ms 104104 KB Output is correct
30 Correct 528 ms 204500 KB Output is correct
31 Correct 24 ms 41116 KB Output is correct
32 Correct 505 ms 110588 KB Output is correct
33 Correct 305 ms 200872 KB Output is correct
34 Correct 135 ms 198472 KB Output is correct
35 Correct 170 ms 200996 KB Output is correct
36 Correct 155 ms 159712 KB Output is correct
37 Correct 75 ms 71116 KB Output is correct
38 Correct 128 ms 100852 KB Output is correct
39 Correct 264 ms 183824 KB Output is correct
40 Correct 260 ms 167712 KB Output is correct
41 Correct 85 ms 72632 KB Output is correct
42 Correct 150 ms 119404 KB Output is correct
43 Correct 308 ms 201044 KB Output is correct
44 Correct 143 ms 198472 KB Output is correct
45 Correct 189 ms 201012 KB Output is correct
46 Correct 160 ms 159800 KB Output is correct
47 Correct 78 ms 65084 KB Output is correct
48 Correct 130 ms 100864 KB Output is correct
49 Correct 236 ms 201040 KB Output is correct
50 Correct 236 ms 202008 KB Output is correct
51 Correct 218 ms 202012 KB Output is correct
52 Correct 257 ms 183700 KB Output is correct
53 Correct 237 ms 167728 KB Output is correct
54 Correct 83 ms 72648 KB Output is correct
55 Correct 159 ms 119468 KB Output is correct
56 Correct 300 ms 201056 KB Output is correct
57 Correct 139 ms 198544 KB Output is correct
58 Correct 168 ms 201048 KB Output is correct
59 Correct 156 ms 159768 KB Output is correct
60 Correct 70 ms 65044 KB Output is correct
61 Correct 129 ms 100876 KB Output is correct
62 Correct 241 ms 201068 KB Output is correct
63 Correct 214 ms 202108 KB Output is correct
64 Correct 218 ms 201904 KB Output is correct
65 Correct 514 ms 139848 KB Output is correct
66 Correct 663 ms 204392 KB Output is correct
67 Correct 715 ms 203076 KB Output is correct
68 Correct 576 ms 166720 KB Output is correct
69 Correct 675 ms 164476 KB Output is correct
70 Correct 242 ms 44600 KB Output is correct
71 Correct 662 ms 204376 KB Output is correct
72 Correct 713 ms 203080 KB Output is correct
73 Correct 587 ms 166596 KB Output is correct
74 Correct 381 ms 193160 KB Output is correct
75 Correct 343 ms 204260 KB Output is correct
76 Correct 358 ms 203004 KB Output is correct
77 Correct 346 ms 166768 KB Output is correct
78 Correct 585 ms 157464 KB Output is correct
79 Correct 946 ms 187168 KB Output is correct
80 Correct 993 ms 171104 KB Output is correct
81 Correct 340 ms 76168 KB Output is correct
82 Correct 434 ms 122992 KB Output is correct
83 Correct 662 ms 204476 KB Output is correct
84 Correct 482 ms 201996 KB Output is correct
85 Correct 466 ms 204508 KB Output is correct
86 Correct 430 ms 163336 KB Output is correct
87 Correct 317 ms 68568 KB Output is correct
88 Correct 342 ms 104344 KB Output is correct
89 Correct 465 ms 204512 KB Output is correct
90 Correct 577 ms 205548 KB Output is correct
91 Correct 479 ms 205388 KB Output is correct