Submission #56326037


Source Code Expand

#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 200005;

int n,y[maxn][2],mny[18][maxn],mxy[18][maxn];

int jump(int sx, int tx, int sy){
    int len = 1;
    for(int b = 17;b >= 0;--b){
        int sz = (1 << b);
        if(sx + len + sz - 1 <= tx){
            if(mny[b][sx + len] <= sy && sy <= mxy[b][sx + len]){
                len += sz;
            }
        }
    }
    return sx + len - 1;
}

struct Data{
    int x,id;
    long long c;
};

Data go[18][maxn][2];

int main(){
    cin >> n;
    for(int i = 0;i < n;++i){
        cin >> y[i][0] >> y[i][1];
    }
    for(int i = 0;i < n;++i){
        mny[0][i] = y[i][0];
        mxy[0][i] = y[i][1];
    }
    for(int b = 1;b < 18;++b){
        int sz = (1 << b);
        int szh = (1 << (b - 1));
        for(int i = 0,mi = i + szh;i + sz - 1 < n;++i, ++mi){
            mny[b][i] = max(mny[b - 1][i], mny[b - 1][mi]);
            mxy[b][i] = min(mxy[b - 1][i], mxy[b - 1][mi]);
        }
    }
    for(int i = 0;i < n;++i){
        for(int j = 0;j < 2;++j){
            int res = jump(i, n - 1, y[i][j]);
            go[0][i][j] = {res + 1, 0, 0};
            if(res < n - 1){
                if(y[i][j] < y[res + 1][0]){
                    go[0][i][j].id = 0;
                    go[0][i][j].c = y[res + 1][0] - y[i][j];
                }else{
                    go[0][i][j].id = 1;
                    go[0][i][j].c = y[i][j] - y[res + 1][1];
                }
            }
        }
    }
    for(int b = 1;b < 18;++b){
        for(int i = 0;i < n;++i){
            for(int j = 0;j < 2;++j){
                if(go[b - 1][i][j].x < n - 1){
                    auto &[x, id, c] = go[b - 1][i][j];
                    auto aux = go[b - 1][x][id];
                    go[b][i][j] = {aux.x, aux.id, c + aux.c};
                }else{
                    go[b][i][j] = {n, 0, 0};
                }
            }
        }
    }
    int nq,sx,sy,tx,ty;
    cin >> nq;
    while(nq--){
        cin >> sx >> sy >> tx >> ty;
        --sx; --tx;
        if(sx == tx){
            cout << abs(sy - ty) << '\n';
            continue;
        }
        if(sx > tx){
            swap(sx, tx);
            swap(sy, ty);
        }
        int tox = jump(sx, tx, sy);
        long long ans = tx - sx;
        if(tox != tx){
            int cur_x = tox + 1,cur_id;
            if(sy < y[cur_x][0]){
                cur_id = 0;
                ans += y[cur_x][0] - sy;
            }else{
                cur_id = 1;
                ans += sy - y[cur_x][1];
            }
            for(int b = 17;b >= 0;--b){
                auto &[x, id, cost] = go[b][cur_x][cur_id];
                if(x <= tx){
                    cur_x = x;
                    cur_id = id;
                    ans += cost;
                }
            }
            ans += abs(y[cur_x][cur_id] - ty);
        }else{
            ans += abs(ty - sy);
        }
        cout << ans << '\n';
    }
    return 0;
}

Submission Info

Submission Time
Task F - Takahashi on Grid
User MarioYC
Language C++ 20 (gcc 12.2)
Score 575
Code Size 3076 Byte
Status AC
Exec Time 899 ms
Memory 144036 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 575 / 575
Status
AC × 3
AC × 63
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 02_handmade_45.txt, 02_handmade_46.txt, 02_handmade_47.txt, 02_handmade_48.txt, 02_handmade_49.txt, 02_handmade_53.txt, 02_handmade_54.txt, 02_handmade_55.txt, 02_handmade_56.txt, 02_handmade_57.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3556 KiB
00_sample_01.txt AC 1 ms 3664 KiB
00_sample_02.txt AC 1 ms 3516 KiB
01_random_03.txt AC 885 ms 143624 KiB
01_random_04.txt AC 899 ms 143616 KiB
01_random_05.txt AC 879 ms 143676 KiB
01_random_06.txt AC 881 ms 143668 KiB
01_random_07.txt AC 895 ms 143668 KiB
01_random_08.txt AC 882 ms 143684 KiB
01_random_09.txt AC 870 ms 143580 KiB
01_random_10.txt AC 868 ms 143636 KiB
01_random_11.txt AC 873 ms 143632 KiB
01_random_12.txt AC 866 ms 143764 KiB
01_random_13.txt AC 858 ms 143684 KiB
01_random_14.txt AC 837 ms 143608 KiB
01_random_15.txt AC 853 ms 143836 KiB
01_random_16.txt AC 816 ms 143608 KiB
01_random_17.txt AC 855 ms 143624 KiB
01_random_18.txt AC 825 ms 143652 KiB
01_random_19.txt AC 858 ms 143620 KiB
01_random_20.txt AC 833 ms 143732 KiB
01_random_21.txt AC 705 ms 83748 KiB
01_random_22.txt AC 195 ms 74192 KiB
01_random_23.txt AC 330 ms 115568 KiB
01_random_24.txt AC 57 ms 6224 KiB
01_random_25.txt AC 170 ms 44484 KiB
01_random_26.txt AC 497 ms 116960 KiB
01_random_27.txt AC 260 ms 80292 KiB
01_random_28.txt AC 179 ms 85136 KiB
01_random_29.txt AC 419 ms 140860 KiB
01_random_30.txt AC 356 ms 111568 KiB
01_random_31.txt AC 97 ms 30332 KiB
01_random_32.txt AC 411 ms 130040 KiB
01_random_33.txt AC 160 ms 85452 KiB
01_random_34.txt AC 243 ms 4820 KiB
01_random_35.txt AC 402 ms 99068 KiB
01_random_36.txt AC 149 ms 71844 KiB
01_random_37.txt AC 317 ms 11024 KiB
01_random_38.txt AC 392 ms 45800 KiB
01_random_39.txt AC 275 ms 81464 KiB
01_random_40.txt AC 130 ms 11176 KiB
01_random_41.txt AC 312 ms 43692 KiB
01_random_42.txt AC 356 ms 49620 KiB
01_random_43.txt AC 335 ms 49200 KiB
01_random_44.txt AC 147 ms 58112 KiB
01_random_45.txt AC 479 ms 94880 KiB
01_random_46.txt AC 480 ms 94792 KiB
01_random_47.txt AC 481 ms 94872 KiB
01_random_48.txt AC 478 ms 94772 KiB
01_random_49.txt AC 476 ms 95040 KiB
01_random_50.txt AC 472 ms 95048 KiB
01_random_51.txt AC 475 ms 94980 KiB
01_random_52.txt AC 476 ms 94964 KiB
02_handmade_45.txt AC 511 ms 144036 KiB
02_handmade_46.txt AC 657 ms 143560 KiB
02_handmade_47.txt AC 528 ms 143608 KiB
02_handmade_48.txt AC 515 ms 143396 KiB
02_handmade_49.txt AC 331 ms 3412 KiB
02_handmade_53.txt AC 503 ms 143936 KiB
02_handmade_54.txt AC 661 ms 143552 KiB
02_handmade_55.txt AC 528 ms 143500 KiB
02_handmade_56.txt AC 516 ms 143520 KiB
02_handmade_57.txt AC 332 ms 3376 KiB