Submission #155292

# Submission time Handle Problem Language Result Execution time Memory
155292 2019-09-27T13:14:45 Z arnold518 Land of the Rainbow Gold (APIO17_rainbow) C++14
100 / 100
1842 ms 140132 KB
#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;

int R, C;

struct SEG
{
    vector<int> tree[MAXN*4+10];

    void push(int node, int tl, int tr, int pos, int val)
    {
        if(tl==tr) { tree[node].push_back(val); return; }
        int mid=tl+tr>>1;
        if(pos<=mid) push(node*2, tl, mid, pos, val);
        else push(node*2+1, mid+1, tr, pos, val);
    }
    void push(int pos, int val) { push(1, 0, C, pos, val); }

    void init(int node, int tl, int tr)
    {
        if(tl==tr)
        {
            sort(tree[node].begin(), tree[node].end());
            return;
        }
        int mid=tl+tr>>1;
        init(node*2, tl, mid);
        init(node*2+1, mid+1, tr);
        tree[node].resize(tree[node*2].size()+tree[node*2+1].size());
        merge(tree[node*2].begin(), tree[node*2].end(), tree[node*2+1].begin(), tree[node*2+1].end(), tree[node].begin());
    }
    void init() { init(1, 0, C); }

    int query(int node, int tl, int tr, int xl, int xr, int yl, int yr)
    {
        if(xl>xr || yl>yr) return 0;
        if(xr<tl || tr<xl) return 0;
        if(xl<=tl && tr<=xr) return upper_bound(tree[node].begin(), tree[node].end(), yr)-lower_bound(tree[node].begin(), tree[node].end(), yl);
        int mid=tl+tr>>1;
        return query(node*2, tl, mid, xl, xr, yl, yr)+query(node*2+1, mid+1, tr, xl, xr, yl, yr);
    }
    int query(int xl, int xr, int yl, int yr) { return query(1, 0, C, xl, xr, yl, yr); }
};
SEG segv, sege1, sege2, segf;

void init(int _R, int _C, int sy, int sx, int M, char *S)
{
    int i, j;
    R=_R; C=_C;

    vector<pii> T, TT;
    T.push_back({sx, sy});
    TT.push_back({sx, sy});
    TT.push_back({sx-1, sy});
    TT.push_back({sx, sy-1});
    TT.push_back({sx-1, sy-1});
    for(i=0; i<M; i++)
    {
        if(S[i]=='N') sy--;
        if(S[i]=='W') sx--;
        if(S[i]=='S') sy++;
        if(S[i]=='E') sx++;
        T.push_back({sx, sy});
        TT.push_back({sx, sy});
        TT.push_back({sx-1, sy});
        TT.push_back({sx, sy-1});
        TT.push_back({sx-1, sy-1});
    }
    sort(T.begin(), T.end());
    T.erase(unique(T.begin(), T.end()), T.end());

    sort(TT.begin(), TT.end());
    TT.erase(unique(TT.begin(), TT.end()), TT.end());

    for(auto it : TT) segv.push(it.first, it.second);
    for(auto it : TT) if(binary_search(T.begin(), T.end(), pii(it.first, it.second)) || binary_search(T.begin(), T.end(), pii(it.first, it.second+1))) sege1.push(it.first, it.second);
    for(auto it : TT) if(binary_search(T.begin(), T.end(), pii(it.first, it.second)) || binary_search(T.begin(), T.end(), pii(it.first+1, it.second))) sege2.push(it.first, it.second);
    for(auto it : T) segf.push(it.first, it.second);

    segv.init();
    sege1.init();
    sege2.init();
    segf.init();
}

int colour(int Y1, int X1, int Y2, int X2)
{
    ll v, e, f, c;
    v=segv.query(X1, X2-1, Y1, Y2-1)+2*((ll)Y2-Y1+X2-X1+2);
    e=sege1.query(X1, X2, Y1, Y2-1)+sege2.query(X1, X2-1, Y1, Y2)+2*((ll)Y2-Y1+X2-X1+2);
    f=segf.query(X1, X2, Y1, Y2);
    if(f==0) c=1;
    else if(segf.query(X1, X2, Y1, Y1) || segf.query(X1, X2, Y2, Y2) || segf.query(X1, X1, Y1, Y2) || segf.query(X2, X2, Y1, Y2)) c=1;
    else c=2;

    //printf("%lld %lld %lld %lld\n", v, e, f, c);
    //printf("============================================\n");
    return c-v+e-f;
}

Compilation message

rainbow.cpp: In member function 'void SEG::push(int, int, int, int, int)':
rainbow.cpp:20:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid=tl+tr>>1;
                 ~~^~~
rainbow.cpp: In member function 'void SEG::init(int, int, int)':
rainbow.cpp:33:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid=tl+tr>>1;
                 ~~^~~
rainbow.cpp: In member function 'int SEG::query(int, int, int, int, int, int, int)':
rainbow.cpp:46:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid=tl+tr>>1;
                 ~~^~~
rainbow.cpp: In function 'void init(int, int, int, int, int, char*)':
rainbow.cpp:55:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
# Verdict Execution time Memory Grader output
1 Correct 71 ms 75512 KB Output is correct
2 Correct 74 ms 75768 KB Output is correct
3 Correct 71 ms 75512 KB Output is correct
4 Correct 72 ms 75640 KB Output is correct
5 Correct 74 ms 75880 KB Output is correct
6 Correct 69 ms 75516 KB Output is correct
7 Correct 69 ms 75408 KB Output is correct
8 Correct 196 ms 75504 KB Output is correct
9 Correct 70 ms 75512 KB Output is correct
10 Correct 69 ms 75432 KB Output is correct
11 Correct 72 ms 75536 KB Output is correct
12 Correct 78 ms 75696 KB Output is correct
13 Correct 75 ms 75768 KB Output is correct
14 Correct 76 ms 75964 KB Output is correct
15 Correct 70 ms 75512 KB Output is correct
16 Correct 81 ms 75512 KB Output is correct
17 Correct 70 ms 75520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 75512 KB Output is correct
2 Correct 70 ms 75520 KB Output is correct
3 Correct 779 ms 115720 KB Output is correct
4 Correct 1080 ms 139860 KB Output is correct
5 Correct 1047 ms 138064 KB Output is correct
6 Correct 740 ms 120624 KB Output is correct
7 Correct 929 ms 120212 KB Output is correct
8 Correct 401 ms 81384 KB Output is correct
9 Correct 1285 ms 139828 KB Output is correct
10 Correct 1188 ms 137944 KB Output is correct
11 Correct 887 ms 120636 KB Output is correct
12 Correct 463 ms 135920 KB Output is correct
13 Correct 538 ms 139876 KB Output is correct
14 Correct 572 ms 137940 KB Output is correct
15 Correct 519 ms 120528 KB Output is correct
16 Correct 745 ms 122312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 75512 KB Output is correct
2 Correct 303 ms 139800 KB Output is correct
3 Correct 210 ms 123036 KB Output is correct
4 Correct 277 ms 129884 KB Output is correct
5 Correct 203 ms 115764 KB Output is correct
6 Correct 147 ms 88144 KB Output is correct
7 Correct 167 ms 96476 KB Output is correct
8 Correct 287 ms 119264 KB Output is correct
9 Correct 262 ms 113544 KB Output is correct
10 Correct 127 ms 83488 KB Output is correct
11 Correct 184 ms 101056 KB Output is correct
12 Correct 304 ms 139884 KB Output is correct
13 Correct 211 ms 123028 KB Output is correct
14 Correct 280 ms 129820 KB Output is correct
15 Correct 204 ms 115676 KB Output is correct
16 Correct 141 ms 86400 KB Output is correct
17 Correct 168 ms 96476 KB Output is correct
18 Correct 255 ms 124224 KB Output is correct
19 Correct 292 ms 132112 KB Output is correct
20 Correct 269 ms 132136 KB Output is correct
21 Correct 288 ms 119260 KB Output is correct
22 Correct 295 ms 113496 KB Output is correct
23 Correct 136 ms 83432 KB Output is correct
24 Correct 193 ms 101284 KB Output is correct
25 Correct 303 ms 140060 KB Output is correct
26 Correct 211 ms 122976 KB Output is correct
27 Correct 305 ms 129820 KB Output is correct
28 Correct 212 ms 115828 KB Output is correct
29 Correct 149 ms 86324 KB Output is correct
30 Correct 167 ms 96432 KB Output is correct
31 Correct 239 ms 124284 KB Output is correct
32 Correct 267 ms 132060 KB Output is correct
33 Correct 284 ms 132240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 75512 KB Output is correct
2 Correct 74 ms 75768 KB Output is correct
3 Correct 71 ms 75512 KB Output is correct
4 Correct 72 ms 75640 KB Output is correct
5 Correct 74 ms 75880 KB Output is correct
6 Correct 69 ms 75516 KB Output is correct
7 Correct 69 ms 75408 KB Output is correct
8 Correct 196 ms 75504 KB Output is correct
9 Correct 70 ms 75512 KB Output is correct
10 Correct 69 ms 75432 KB Output is correct
11 Correct 72 ms 75536 KB Output is correct
12 Correct 78 ms 75696 KB Output is correct
13 Correct 75 ms 75768 KB Output is correct
14 Correct 76 ms 75964 KB Output is correct
15 Correct 70 ms 75512 KB Output is correct
16 Correct 81 ms 75512 KB Output is correct
17 Correct 70 ms 75520 KB Output is correct
18 Correct 1073 ms 93388 KB Output is correct
19 Correct 441 ms 79632 KB Output is correct
20 Correct 267 ms 78968 KB Output is correct
21 Correct 293 ms 79220 KB Output is correct
22 Correct 313 ms 79400 KB Output is correct
23 Correct 390 ms 79908 KB Output is correct
24 Correct 318 ms 79048 KB Output is correct
25 Correct 334 ms 79248 KB Output is correct
26 Correct 327 ms 79284 KB Output is correct
27 Correct 548 ms 90908 KB Output is correct
28 Correct 576 ms 85272 KB Output is correct
29 Correct 638 ms 89940 KB Output is correct
30 Correct 927 ms 106512 KB Output is correct
31 Correct 74 ms 75512 KB Output is correct
32 Correct 1022 ms 91648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 75512 KB Output is correct
2 Correct 74 ms 75768 KB Output is correct
3 Correct 71 ms 75512 KB Output is correct
4 Correct 72 ms 75640 KB Output is correct
5 Correct 74 ms 75880 KB Output is correct
6 Correct 69 ms 75516 KB Output is correct
7 Correct 69 ms 75408 KB Output is correct
8 Correct 196 ms 75504 KB Output is correct
9 Correct 70 ms 75512 KB Output is correct
10 Correct 69 ms 75432 KB Output is correct
11 Correct 72 ms 75536 KB Output is correct
12 Correct 78 ms 75696 KB Output is correct
13 Correct 75 ms 75768 KB Output is correct
14 Correct 76 ms 75964 KB Output is correct
15 Correct 70 ms 75512 KB Output is correct
16 Correct 81 ms 75512 KB Output is correct
17 Correct 70 ms 75520 KB Output is correct
18 Correct 1073 ms 93388 KB Output is correct
19 Correct 441 ms 79632 KB Output is correct
20 Correct 267 ms 78968 KB Output is correct
21 Correct 293 ms 79220 KB Output is correct
22 Correct 313 ms 79400 KB Output is correct
23 Correct 390 ms 79908 KB Output is correct
24 Correct 318 ms 79048 KB Output is correct
25 Correct 334 ms 79248 KB Output is correct
26 Correct 327 ms 79284 KB Output is correct
27 Correct 548 ms 90908 KB Output is correct
28 Correct 576 ms 85272 KB Output is correct
29 Correct 638 ms 89940 KB Output is correct
30 Correct 927 ms 106512 KB Output is correct
31 Correct 74 ms 75512 KB Output is correct
32 Correct 1022 ms 91648 KB Output is correct
33 Correct 303 ms 139800 KB Output is correct
34 Correct 210 ms 123036 KB Output is correct
35 Correct 277 ms 129884 KB Output is correct
36 Correct 203 ms 115764 KB Output is correct
37 Correct 147 ms 88144 KB Output is correct
38 Correct 167 ms 96476 KB Output is correct
39 Correct 287 ms 119264 KB Output is correct
40 Correct 262 ms 113544 KB Output is correct
41 Correct 127 ms 83488 KB Output is correct
42 Correct 184 ms 101056 KB Output is correct
43 Correct 304 ms 139884 KB Output is correct
44 Correct 211 ms 123028 KB Output is correct
45 Correct 280 ms 129820 KB Output is correct
46 Correct 204 ms 115676 KB Output is correct
47 Correct 141 ms 86400 KB Output is correct
48 Correct 168 ms 96476 KB Output is correct
49 Correct 255 ms 124224 KB Output is correct
50 Correct 292 ms 132112 KB Output is correct
51 Correct 269 ms 132136 KB Output is correct
52 Correct 288 ms 119260 KB Output is correct
53 Correct 295 ms 113496 KB Output is correct
54 Correct 136 ms 83432 KB Output is correct
55 Correct 193 ms 101284 KB Output is correct
56 Correct 303 ms 140060 KB Output is correct
57 Correct 211 ms 122976 KB Output is correct
58 Correct 305 ms 129820 KB Output is correct
59 Correct 212 ms 115828 KB Output is correct
60 Correct 149 ms 86324 KB Output is correct
61 Correct 167 ms 96432 KB Output is correct
62 Correct 239 ms 124284 KB Output is correct
63 Correct 267 ms 132060 KB Output is correct
64 Correct 284 ms 132240 KB Output is correct
65 Correct 1842 ms 119644 KB Output is correct
66 Correct 1837 ms 113748 KB Output is correct
67 Correct 848 ms 85528 KB Output is correct
68 Correct 1069 ms 101708 KB Output is correct
69 Correct 1177 ms 140132 KB Output is correct
70 Correct 617 ms 123464 KB Output is correct
71 Correct 760 ms 130272 KB Output is correct
72 Correct 687 ms 116624 KB Output is correct
73 Correct 527 ms 86940 KB Output is correct
74 Correct 596 ms 96656 KB Output is correct
75 Correct 574 ms 124688 KB Output is correct
76 Correct 913 ms 132492 KB Output is correct
77 Correct 1008 ms 132528 KB Output is correct
78 Correct 779 ms 115720 KB Output is correct
79 Correct 1080 ms 139860 KB Output is correct
80 Correct 1047 ms 138064 KB Output is correct
81 Correct 740 ms 120624 KB Output is correct
82 Correct 929 ms 120212 KB Output is correct
83 Correct 401 ms 81384 KB Output is correct
84 Correct 1285 ms 139828 KB Output is correct
85 Correct 1188 ms 137944 KB Output is correct
86 Correct 887 ms 120636 KB Output is correct
87 Correct 463 ms 135920 KB Output is correct
88 Correct 538 ms 139876 KB Output is correct
89 Correct 572 ms 137940 KB Output is correct
90 Correct 519 ms 120528 KB Output is correct
91 Correct 745 ms 122312 KB Output is correct