답안 #780364

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
780364 2023-07-12T08:31:59 Z 이동현(#10007) Railway Trip (JOI17_railway_trip) C++17
100 / 100
431 ms 99004 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

const int NS = (int)1e5 + 4, SZ = (int)3e5 + 4;
int a[NS + 4];
int seg[NS * 4 + 4];
int spa[SZ][20][2][2], dep[SZ], lcnt[SZ], pr[SZ][20];
vector<int> pos[NS];
void build(int x, int s, int e){
    if(s == e){
        seg[x] = a[s];
        return;
    }
    int m = s + e >> 1;
    build(x * 2, s, m);
    build(x * 2 + 1, m + 1, e);
    seg[x] = max(seg[x * 2], seg[x * 2 + 1]);
}

int get(int x, int s, int e, int fs, int fe){
    if(fe < s || fs > e || fs > fe) return -1;
    if(fs <= s && fe >= e){
        return seg[x];
    }
    int m = s + e >> 1;
    return max(get(x * 2, s, m, fs, fe), get(x * 2 + 1, m + 1, e, fs, fe));
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, k, q;
    cin >> n >> k >> q;
    a[0] = a[n + 1] = k + 1;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    n += 2;
    build(1, 0, n - 1);
    for(int i = 0; i < n; ++i){
        pos[a[i]].push_back(i);
    }

    vector<int> st(n);
    int tn = 0;
    auto gen = [&](auto&&self, int x, int s, int e)->void{
        // cout << x << ' ' << s << ' ' << e << endl;
        if(s + 1 >= e){
            st[s] = x;
            return;
        }
        int mx = get(1, 0, n - 1, s + 1, e - 1);

        vector<int> id;
        int now = s;
        while(true){
            now = lower_bound(pos[mx].begin(), pos[mx].end(), now + 1) - pos[mx].begin();
            if(now < (int)pos[mx].size() && pos[mx][now] < e){
                now = pos[mx][now];
                id.push_back(now);
            }
            else break;
        }

        for(int i = 0; i <= (int)id.size(); ++i){
            ++tn;
            dep[tn] = dep[x] + 1;
            lcnt[tn] = i;
            pr[tn][0] = x;
            spa[tn][0][0][0] = min(i, (int)id.size() - i + 2);
            spa[tn][0][0][1] = min(i + 1, (int)id.size() - i + 1);
            spa[tn][0][1][0] = min(i + 1, (int)id.size() - i + 1);
            spa[tn][0][1][1] = min(i + 2, (int)id.size() - i);

            self(self, tn, (i ? id[i - 1] : s), (i < (int)id.size() ? id[i] : e));
        }
    };
    gen(gen, 0, 0, n - 1);

    for(int j = 1; j < 20; ++j){
        for(int i = 0; i <= tn; ++i){
            pr[i][j] = pr[pr[i][j - 1]][j - 1];
            spa[i][j][0][0] = min(spa[i][j - 1][0][0] + spa[pr[i][j - 1]][j - 1][0][0], spa[i][j - 1][0][1] + spa[pr[i][j - 1]][j - 1][1][0]);
            spa[i][j][0][1] = min(spa[i][j - 1][0][0] + spa[pr[i][j - 1]][j - 1][0][1], spa[i][j - 1][0][1] + spa[pr[i][j - 1]][j - 1][1][1]);
            spa[i][j][1][0] = min(spa[i][j - 1][1][0] + spa[pr[i][j - 1]][j - 1][0][0], spa[i][j - 1][1][1] + spa[pr[i][j - 1]][j - 1][1][0]);
            spa[i][j][1][1] = min(spa[i][j - 1][1][0] + spa[pr[i][j - 1]][j - 1][0][1], spa[i][j - 1][1][1] + spa[pr[i][j - 1]][j - 1][1][1]);
        }
    }

    while(q--){
        int x, y;
        cin >> x >> y;
        x = st[x], y = st[y];
        
        if(dep[x] > dep[y]){
            swap(x, y);
        }
        vector<int> vx = {0, 1}, vy = {0, 1};
        for(int i = 19; i >= 0; --i){
            if(dep[y] - (1 << i) >= dep[x]){
                vector<int> nxt = {min(vy[0] + spa[y][i][0][0], vy[1] + spa[y][i][1][0]), min(vy[0] + spa[y][i][0][1], vy[1] + spa[y][i][1][1])};
                vy = nxt;
                y = pr[y][i];
            }
        }

        for(int i = 19; i >= 0; --i){
            if(pr[x][i] != pr[y][i]){
                vector<int> nxt = {min(vy[0] + spa[y][i][0][0], vy[1] + spa[y][i][1][0]), min(vy[0] + spa[y][i][0][1], vy[1] + spa[y][i][1][1])};
                vy = nxt;
                y = pr[y][i];
                
                nxt = {min(vx[0] + spa[x][i][0][0], vx[1] + spa[x][i][1][0]), min(vx[0] + spa[x][i][0][1], vx[1] + spa[x][i][1][1])};
                vx = nxt;
                x = pr[x][i];
            }
        }

        // cout << x << ' ' << y << ' ' << vx[0] << ' ' << vx[1] << ' ' << vy[0] << ' ' << vy[1] << endl;
        int ans;
        if(x == y) ans = min(vx[0] + vy[0], vx[1] + vy[1]);
        else{
            if(lcnt[x] < lcnt[y]) ans = vx[1] + vy[0] + lcnt[y] - lcnt[x] - 1;
            else ans = vx[0] + vy[1] + lcnt[x] - lcnt[y] - 1;
            if(pr[x][0] > 0){
                ans = min(ans, vx[0] + spa[x][0][0][0] + vy[1] + spa[y][0][1][1] + 1);
                ans = min(ans, vx[1] + spa[x][0][1][1] + vy[0] + spa[y][0][0][0] + 1);
            }
        }

        cout << ans - 1 << endl;
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2784 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2692 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3540 KB Output is correct
2 Correct 125 ms 73468 KB Output is correct
3 Correct 124 ms 78040 KB Output is correct
4 Correct 135 ms 81592 KB Output is correct
5 Correct 132 ms 83100 KB Output is correct
6 Correct 146 ms 85040 KB Output is correct
7 Correct 147 ms 86920 KB Output is correct
8 Correct 63 ms 45480 KB Output is correct
9 Correct 108 ms 63700 KB Output is correct
10 Correct 81 ms 56608 KB Output is correct
11 Correct 102 ms 64460 KB Output is correct
12 Correct 102 ms 64300 KB Output is correct
13 Correct 102 ms 64036 KB Output is correct
14 Correct 109 ms 64432 KB Output is correct
15 Correct 102 ms 64312 KB Output is correct
16 Correct 101 ms 64080 KB Output is correct
17 Correct 143 ms 85528 KB Output is correct
18 Correct 142 ms 85532 KB Output is correct
19 Correct 148 ms 88048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 341 ms 77332 KB Output is correct
2 Correct 295 ms 75216 KB Output is correct
3 Correct 297 ms 77880 KB Output is correct
4 Correct 301 ms 78312 KB Output is correct
5 Correct 304 ms 78708 KB Output is correct
6 Correct 297 ms 79292 KB Output is correct
7 Correct 316 ms 79580 KB Output is correct
8 Correct 226 ms 56844 KB Output is correct
9 Correct 208 ms 46856 KB Output is correct
10 Correct 209 ms 46928 KB Output is correct
11 Correct 198 ms 46784 KB Output is correct
12 Correct 211 ms 46856 KB Output is correct
13 Correct 217 ms 46892 KB Output is correct
14 Correct 226 ms 46948 KB Output is correct
15 Correct 224 ms 47672 KB Output is correct
16 Correct 252 ms 54548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 328 ms 88300 KB Output is correct
2 Correct 371 ms 88256 KB Output is correct
3 Correct 325 ms 87052 KB Output is correct
4 Correct 325 ms 87360 KB Output is correct
5 Correct 199 ms 46848 KB Output is correct
6 Correct 320 ms 65484 KB Output is correct
7 Correct 314 ms 65452 KB Output is correct
8 Correct 308 ms 58188 KB Output is correct
9 Correct 314 ms 66108 KB Output is correct
10 Correct 312 ms 65948 KB Output is correct
11 Correct 318 ms 65608 KB Output is correct
12 Correct 308 ms 66008 KB Output is correct
13 Correct 325 ms 65896 KB Output is correct
14 Correct 392 ms 82152 KB Output is correct
15 Correct 411 ms 88744 KB Output is correct
16 Correct 431 ms 99004 KB Output is correct
17 Correct 353 ms 75212 KB Output is correct
18 Correct 368 ms 75904 KB Output is correct
19 Correct 348 ms 75952 KB Output is correct
20 Correct 345 ms 72384 KB Output is correct
21 Correct 320 ms 86948 KB Output is correct
22 Correct 353 ms 86976 KB Output is correct
23 Correct 344 ms 89304 KB Output is correct