답안 #918924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
918924 2024-01-30T20:32:53 Z esomer Bitaro's travel (JOI23_travel) C++17
100 / 100
313 ms 40248 KB
#include <bits/stdc++.h>

using namespace std;

int msb[200001];

int getMax(int l, int r, vector<vector<int>>& dp){
    int k = msb[r - l + 1];
    return max(dp[k][r], dp[k][l + (1 << k) - 1]);
}

int getMin(int l, int r, vector<vector<int>>& dp){
    int k = msb[r - l + 1];
    return min(dp[k][r], dp[k][l + (1 << k) - 1]);
}

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

    int n; cin >> n;
    vector<int> X(n);
    for(auto &i : X) cin >> i;
    vector<vector<int>> goLeft(20, vector<int> (n, 2e9+1));
    vector<vector<int>> goRight(20, vector<int> (n, -2e9-1));
    for(int k = 0; k < 20; k++){
        for(int i = 0; i < n; i++){
            if(k == 0){
                if(i == 0 || i == n - 1) continue;
                goLeft[k][i] = 2 * X[i] - X[i-1];
                goRight[k][i] = 2 * X[i] - X[i+1];
            }else{
                if(i - (1 << (k-1)) < 0){
                    goLeft[k][i] = goLeft[k-1][i];
                    goRight[k][i] = goRight[k-1][i];
                }else{
                    goLeft[k][i] = max(goLeft[k-1][i], goLeft[k-1][i-(1<<(k-1))]);
                    goRight[k][i] = min(goRight[k-1][i], goRight[k-1][i-(1<<(k-1))]);
                }
            }
        }
    }
    int currExp = 1;
    int exp = -1;
    for(int i = 1; i <= n; i++){
        if(i == currExp){
            exp++;
            currExp *= 2;
        }
        msb[i] = exp;
    }
    int q; cin >> q;
    while(q--){
        int x; cin >> x;
        int lo = 0;
        int hi = n - 1;
        int bst = -1;
        while(lo <= hi){
            int mid = (lo + hi) / 2;
            if(X[mid] <= x){
                bst = mid;
                lo = mid + 1;
            }else hi = mid - 1;
        }
        long long dist = 0;
        if(bst == -1){
            dist = X[0] - x;
            x = 0;
        }else if(bst == n-1){
            dist = x - X[n-1];
            x = n - 1;
        }else{
            if(x - X[bst] <= X[bst+1]-x){
                dist = x - X[bst];
                x = bst;
            }else{
                dist = X[bst+1] - x;
                x = bst + 1;
            }
        }
        int l = x, r = x;
        while(l != 0 && r != n - 1){
            if(X[x] - X[l - 1] <= X[r+1] - X[x]){
                lo = 0;
                hi = l - 1;
                bst = -1;
                while(lo <= hi){
                    int mid = (lo + hi) / 2;
                    if(getMax(mid, l - 1, goLeft) > X[r+1]){
                        bst = mid;
                        lo = mid + 1;
                    }else hi = mid - 1;
                }
                dist += X[x] - X[bst];
                l = bst;
                x = bst;
            }else{
                lo = r+1;
                hi = n - 1;
                bst = -1;
                while(lo <= hi){
                    int mid = (lo + hi) / 2;
                    if(X[l-1] >= getMin(r+1, mid, goRight)){
                        bst = mid;
                        hi = mid - 1;
                    }else lo = mid + 1;
                }
                dist += X[bst] - X[x];
                r = bst;
                x = bst;
            }
        }
        dist += X[n-1] - X[0];
        cout << dist << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 732 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 732 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 41 ms 35904 KB Output is correct
18 Correct 40 ms 35900 KB Output is correct
19 Correct 41 ms 35900 KB Output is correct
20 Correct 39 ms 36072 KB Output is correct
21 Correct 39 ms 35900 KB Output is correct
22 Correct 42 ms 36100 KB Output is correct
23 Correct 40 ms 35904 KB Output is correct
24 Correct 40 ms 36112 KB Output is correct
25 Correct 39 ms 36036 KB Output is correct
26 Correct 39 ms 35908 KB Output is correct
27 Correct 39 ms 35904 KB Output is correct
28 Correct 40 ms 35904 KB Output is correct
29 Correct 40 ms 36048 KB Output is correct
30 Correct 42 ms 36044 KB Output is correct
31 Correct 39 ms 35996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 103 ms 38092 KB Output is correct
8 Correct 103 ms 37952 KB Output is correct
9 Correct 108 ms 37952 KB Output is correct
10 Correct 101 ms 37960 KB Output is correct
11 Correct 109 ms 38248 KB Output is correct
12 Correct 106 ms 37948 KB Output is correct
13 Correct 31 ms 4176 KB Output is correct
14 Correct 24 ms 1616 KB Output is correct
15 Correct 111 ms 38976 KB Output is correct
16 Correct 121 ms 39068 KB Output is correct
17 Correct 106 ms 39088 KB Output is correct
18 Correct 34 ms 4176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 732 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 41 ms 35904 KB Output is correct
18 Correct 40 ms 35900 KB Output is correct
19 Correct 41 ms 35900 KB Output is correct
20 Correct 39 ms 36072 KB Output is correct
21 Correct 39 ms 35900 KB Output is correct
22 Correct 42 ms 36100 KB Output is correct
23 Correct 40 ms 35904 KB Output is correct
24 Correct 40 ms 36112 KB Output is correct
25 Correct 39 ms 36036 KB Output is correct
26 Correct 39 ms 35908 KB Output is correct
27 Correct 39 ms 35904 KB Output is correct
28 Correct 40 ms 35904 KB Output is correct
29 Correct 40 ms 36048 KB Output is correct
30 Correct 42 ms 36044 KB Output is correct
31 Correct 39 ms 35996 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 103 ms 38092 KB Output is correct
39 Correct 103 ms 37952 KB Output is correct
40 Correct 108 ms 37952 KB Output is correct
41 Correct 101 ms 37960 KB Output is correct
42 Correct 109 ms 38248 KB Output is correct
43 Correct 106 ms 37948 KB Output is correct
44 Correct 31 ms 4176 KB Output is correct
45 Correct 24 ms 1616 KB Output is correct
46 Correct 111 ms 38976 KB Output is correct
47 Correct 121 ms 39068 KB Output is correct
48 Correct 106 ms 39088 KB Output is correct
49 Correct 34 ms 4176 KB Output is correct
50 Correct 313 ms 40160 KB Output is correct
51 Correct 261 ms 39996 KB Output is correct
52 Correct 241 ms 40184 KB Output is correct
53 Correct 177 ms 40248 KB Output is correct
54 Correct 175 ms 40192 KB Output is correct
55 Correct 180 ms 39996 KB Output is correct
56 Correct 93 ms 40060 KB Output is correct
57 Correct 94 ms 40000 KB Output is correct
58 Correct 103 ms 40000 KB Output is correct