Submission #384186

# Submission time Handle Problem Language Result Execution time Memory
384186 2021-03-31T17:08:39 Z BartolM Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
1406 ms 172524 KB
#define DEBUG 0
#include "rainbow.h"
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=2e5+5;
const int OFF=(1<<18);

int uk;
int lef[150*N], rig[150*N], val[150*N];
vector <int> v_br[N], v_v[N], v_ok[N], v_vod[N];

int novi(int x, int l, int r) {
    lef[uk]=l; rig[uk]=r;
    val[uk]=x;
    return uk++;
}

int upd(int ind, int pos, int lo, int hi) {
    if (ind>=hi || ind<lo) return pos;
    if (lo==hi-1) return novi(1+val[pos], 0, 0);
    int mid=(lo+hi)/2;
    return novi(1+val[pos], upd(ind, lef[pos], lo, mid), upd(ind, rig[pos], mid, hi));
}

int que(int a, int b, int pos, int lo, int hi) {
    if (!pos) return 0;
    if (lo>=b || hi<=a) return 0;
    if (lo>=a && hi<=b) return val[pos];
    int mid=(lo+hi)/2;
    return que(a, b, lef[pos], lo, mid)+que(a, b, rig[pos], mid, hi);
}

struct Tour{
    int T[N];
    Tour() {
        memset(T, 0, sizeof T);
    }
    int& operator [] (int x) {
        return T[x];
    }
    void update(int r, int c) {
        T[r]=upd(c, T[r], 0, OFF);
    }
    int query(int r1, int c1, int r2, int c2) {
        return que(c1, c2+1, T[r2], 0, OFF)-que(c1, c2+1, T[r1-1], 0, OFF);
    }
};

int mxr, mnr, mxc, mnc;

void dodaj(int r, int c) {
    v_br[r].pb(c);

    v_v[r].pb(c); v_v[r].pb(c+1);
    v_v[r+1].pb(c); v_v[r+1].pb(c+1);

    v_ok[r].pb(c); v_ok[r].pb(c+1);
    v_vod[r].pb(c); v_vod[r+1].pb(c);
    mxr=max(mxr, r); mxc=max(mxc, c);
    mnr=min(mnr, r); mnc=min(mnc, c);
}

void sazmi(vector <int> &v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}

Tour Tvod, Tok, Tv, Tbr;

void init(int R, int C, int sr, int sc, int M, char *S) {
    uk=1;
    mxr=mnr=sr; mxc=mnc=sc;
    dodaj(sr, sc);
    for (int i=0; i<M; ++i) {
        if (S[i]=='N') sr--;
        if (S[i]=='S') sr++;
        if (S[i]=='W') sc--;
        if (S[i]=='E') sc++;
        dodaj(sr, sc);
    }
    for (int i=1; i<=R+1; ++i) {
        sazmi(v_br[i]); sazmi(v_v[i]); sazmi(v_ok[i]); sazmi(v_vod[i]);
        Tbr[i]=Tbr[i-1]; Tv[i]=Tv[i-1]; Tok[i]=Tok[i-1]; Tvod[i]=Tvod[i-1];
        for (int x:v_br[i]) Tbr.update(i, x);
        for (int x:v_v[i]) Tv.update(i, x);
        for (int x:v_ok[i]) Tok.update(i, x);
        for (int x:v_vod[i]) Tvod.update(i, x);
    }
}

int colour(int r1, int c1, int r2, int c2) {
    int red=r2-r1+1, col=c2-c1+1;

    int v=Tv.query(r1+1, c1+1, r2, c2)+2*(red+col);
    int e=Tok.query(r1, c1+1, r2, c2)+Tvod.query(r1+1, c1, r2, c2)+2*(red+col);
    int comp=1;
    if (r1<mnr && r2>mxr && c1<mnc && c2>mxc) ++comp;
    int f=1-v+e+comp;
    int br=Tbr.query(r1, c1, r2, c2);
#if DEBUG
    printf("v: %d, e: %d, comp: %d\nf: %d, br: %d\n", v, e, comp, f, br);
#endif // DEBUG
    return f-br-1;
}

# Verdict Execution time Memory Grader output
1 Correct 18 ms 22508 KB Output is correct
2 Correct 20 ms 23552 KB Output is correct
3 Correct 19 ms 22636 KB Output is correct
4 Correct 18 ms 22636 KB Output is correct
5 Correct 21 ms 23788 KB Output is correct
6 Correct 15 ms 22252 KB Output is correct
7 Correct 16 ms 22252 KB Output is correct
8 Correct 16 ms 22252 KB Output is correct
9 Correct 16 ms 22252 KB Output is correct
10 Correct 16 ms 22252 KB Output is correct
11 Correct 20 ms 22764 KB Output is correct
12 Correct 19 ms 23276 KB Output is correct
13 Correct 22 ms 24172 KB Output is correct
14 Correct 24 ms 24684 KB Output is correct
15 Correct 16 ms 22252 KB Output is correct
16 Correct 16 ms 22252 KB Output is correct
17 Correct 17 ms 22252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 22252 KB Output is correct
2 Correct 17 ms 22252 KB Output is correct
3 Correct 436 ms 108384 KB Output is correct
4 Correct 711 ms 163936 KB Output is correct
5 Correct 713 ms 162772 KB Output is correct
6 Correct 491 ms 132692 KB Output is correct
7 Correct 688 ms 130572 KB Output is correct
8 Correct 111 ms 30424 KB Output is correct
9 Correct 734 ms 163792 KB Output is correct
10 Correct 738 ms 163028 KB Output is correct
11 Correct 551 ms 132560 KB Output is correct
12 Correct 334 ms 154836 KB Output is correct
13 Correct 415 ms 163920 KB Output is correct
14 Correct 442 ms 162768 KB Output is correct
15 Correct 395 ms 132816 KB Output is correct
16 Correct 496 ms 123724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 22252 KB Output is correct
2 Correct 271 ms 160544 KB Output is correct
3 Correct 276 ms 168940 KB Output is correct
4 Correct 272 ms 163308 KB Output is correct
5 Correct 230 ms 130480 KB Output is correct
6 Correct 99 ms 52780 KB Output is correct
7 Correct 141 ms 77420 KB Output is correct
8 Correct 274 ms 146776 KB Output is correct
9 Correct 258 ms 133224 KB Output is correct
10 Correct 92 ms 51188 KB Output is correct
11 Correct 182 ms 92884 KB Output is correct
12 Correct 271 ms 160800 KB Output is correct
13 Correct 282 ms 168812 KB Output is correct
14 Correct 273 ms 163564 KB Output is correct
15 Correct 232 ms 130480 KB Output is correct
16 Correct 88 ms 47340 KB Output is correct
17 Correct 142 ms 77804 KB Output is correct
18 Correct 278 ms 161132 KB Output is correct
19 Correct 287 ms 164400 KB Output is correct
20 Correct 280 ms 164400 KB Output is correct
21 Correct 277 ms 146648 KB Output is correct
22 Correct 258 ms 133160 KB Output is correct
23 Correct 88 ms 51180 KB Output is correct
24 Correct 178 ms 92756 KB Output is correct
25 Correct 273 ms 160544 KB Output is correct
26 Correct 277 ms 168812 KB Output is correct
27 Correct 272 ms 163308 KB Output is correct
28 Correct 232 ms 130608 KB Output is correct
29 Correct 88 ms 47340 KB Output is correct
30 Correct 140 ms 77804 KB Output is correct
31 Correct 278 ms 161132 KB Output is correct
32 Correct 277 ms 164400 KB Output is correct
33 Correct 279 ms 164444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 22508 KB Output is correct
2 Correct 20 ms 23552 KB Output is correct
3 Correct 19 ms 22636 KB Output is correct
4 Correct 18 ms 22636 KB Output is correct
5 Correct 21 ms 23788 KB Output is correct
6 Correct 15 ms 22252 KB Output is correct
7 Correct 16 ms 22252 KB Output is correct
8 Correct 16 ms 22252 KB Output is correct
9 Correct 16 ms 22252 KB Output is correct
10 Correct 16 ms 22252 KB Output is correct
11 Correct 20 ms 22764 KB Output is correct
12 Correct 19 ms 23276 KB Output is correct
13 Correct 22 ms 24172 KB Output is correct
14 Correct 24 ms 24684 KB Output is correct
15 Correct 16 ms 22252 KB Output is correct
16 Correct 16 ms 22252 KB Output is correct
17 Correct 17 ms 22252 KB Output is correct
18 Correct 1087 ms 98744 KB Output is correct
19 Correct 310 ms 29036 KB Output is correct
20 Correct 244 ms 26428 KB Output is correct
21 Correct 267 ms 26860 KB Output is correct
22 Correct 284 ms 27708 KB Output is correct
23 Correct 299 ms 28780 KB Output is correct
24 Correct 249 ms 26860 KB Output is correct
25 Correct 269 ms 27372 KB Output is correct
26 Correct 314 ms 27884 KB Output is correct
27 Correct 362 ms 85612 KB Output is correct
28 Correct 302 ms 57312 KB Output is correct
29 Correct 340 ms 81004 KB Output is correct
30 Correct 686 ms 164716 KB Output is correct
31 Correct 21 ms 22380 KB Output is correct
32 Correct 785 ms 85612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 22508 KB Output is correct
2 Correct 20 ms 23552 KB Output is correct
3 Correct 19 ms 22636 KB Output is correct
4 Correct 18 ms 22636 KB Output is correct
5 Correct 21 ms 23788 KB Output is correct
6 Correct 15 ms 22252 KB Output is correct
7 Correct 16 ms 22252 KB Output is correct
8 Correct 16 ms 22252 KB Output is correct
9 Correct 16 ms 22252 KB Output is correct
10 Correct 16 ms 22252 KB Output is correct
11 Correct 20 ms 22764 KB Output is correct
12 Correct 19 ms 23276 KB Output is correct
13 Correct 22 ms 24172 KB Output is correct
14 Correct 24 ms 24684 KB Output is correct
15 Correct 16 ms 22252 KB Output is correct
16 Correct 16 ms 22252 KB Output is correct
17 Correct 17 ms 22252 KB Output is correct
18 Correct 1087 ms 98744 KB Output is correct
19 Correct 310 ms 29036 KB Output is correct
20 Correct 244 ms 26428 KB Output is correct
21 Correct 267 ms 26860 KB Output is correct
22 Correct 284 ms 27708 KB Output is correct
23 Correct 299 ms 28780 KB Output is correct
24 Correct 249 ms 26860 KB Output is correct
25 Correct 269 ms 27372 KB Output is correct
26 Correct 314 ms 27884 KB Output is correct
27 Correct 362 ms 85612 KB Output is correct
28 Correct 302 ms 57312 KB Output is correct
29 Correct 340 ms 81004 KB Output is correct
30 Correct 686 ms 164716 KB Output is correct
31 Correct 21 ms 22380 KB Output is correct
32 Correct 785 ms 85612 KB Output is correct
33 Correct 271 ms 160544 KB Output is correct
34 Correct 276 ms 168940 KB Output is correct
35 Correct 272 ms 163308 KB Output is correct
36 Correct 230 ms 130480 KB Output is correct
37 Correct 99 ms 52780 KB Output is correct
38 Correct 141 ms 77420 KB Output is correct
39 Correct 274 ms 146776 KB Output is correct
40 Correct 258 ms 133224 KB Output is correct
41 Correct 92 ms 51188 KB Output is correct
42 Correct 182 ms 92884 KB Output is correct
43 Correct 271 ms 160800 KB Output is correct
44 Correct 282 ms 168812 KB Output is correct
45 Correct 273 ms 163564 KB Output is correct
46 Correct 232 ms 130480 KB Output is correct
47 Correct 88 ms 47340 KB Output is correct
48 Correct 142 ms 77804 KB Output is correct
49 Correct 278 ms 161132 KB Output is correct
50 Correct 287 ms 164400 KB Output is correct
51 Correct 280 ms 164400 KB Output is correct
52 Correct 277 ms 146648 KB Output is correct
53 Correct 258 ms 133160 KB Output is correct
54 Correct 88 ms 51180 KB Output is correct
55 Correct 178 ms 92756 KB Output is correct
56 Correct 273 ms 160544 KB Output is correct
57 Correct 277 ms 168812 KB Output is correct
58 Correct 272 ms 163308 KB Output is correct
59 Correct 232 ms 130608 KB Output is correct
60 Correct 88 ms 47340 KB Output is correct
61 Correct 140 ms 77804 KB Output is correct
62 Correct 278 ms 161132 KB Output is correct
63 Correct 277 ms 164400 KB Output is correct
64 Correct 279 ms 164444 KB Output is correct
65 Correct 436 ms 108384 KB Output is correct
66 Correct 711 ms 163936 KB Output is correct
67 Correct 713 ms 162772 KB Output is correct
68 Correct 491 ms 132692 KB Output is correct
69 Correct 688 ms 130572 KB Output is correct
70 Correct 111 ms 30424 KB Output is correct
71 Correct 734 ms 163792 KB Output is correct
72 Correct 738 ms 163028 KB Output is correct
73 Correct 551 ms 132560 KB Output is correct
74 Correct 334 ms 154836 KB Output is correct
75 Correct 415 ms 163920 KB Output is correct
76 Correct 442 ms 162768 KB Output is correct
77 Correct 395 ms 132816 KB Output is correct
78 Correct 496 ms 123724 KB Output is correct
79 Correct 1364 ms 150488 KB Output is correct
80 Correct 1406 ms 136744 KB Output is correct
81 Correct 494 ms 54764 KB Output is correct
82 Correct 468 ms 96340 KB Output is correct
83 Correct 654 ms 164000 KB Output is correct
84 Correct 438 ms 172524 KB Output is correct
85 Correct 490 ms 166892 KB Output is correct
86 Correct 441 ms 134192 KB Output is correct
87 Correct 174 ms 50796 KB Output is correct
88 Correct 246 ms 81388 KB Output is correct
89 Correct 379 ms 164716 KB Output is correct
90 Correct 633 ms 167984 KB Output is correct
91 Correct 604 ms 168112 KB Output is correct