답안 #916843

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
916843 2024-01-26T15:16:31 Z thangdz2k7 Bitaro's travel (JOI23_travel) C++17
5 / 100
646 ms 50332 KB
#include <bits/stdc++.h>
#define ll long long


using namespace std;

const int N = 2e5 + 5;

int n, x[N];
int q;

int Max[N][20];
int Min[N][20];
int LG[N];

void prepare(){
    for (int i = n; i >= 1; -- i){
        Max[i][0] = 2 * x[i] - x[i - 1];
        Min[i][0] = 2 * x[i] - x[i + 1];
        for (int j = 1; j < 20; ++ j){
            int u = i + (1 << (j - 1));
            if (u > n) break;
            Max[i][j] = max(Max[i][j - 1], Max[u][j - 1]);
            Min[i][j] = min(Min[i][j - 1], Min[u][j - 1]);
        }
        LG[i] = log2(i);
    }
}

int getmax(int l, int r){
    int lg = LG[r - l + 1];
    return max(Max[l][lg], Max[r - (1 << lg) + 1][lg]);
}

int getmin(int l, int r){
    int lg = LG[r - l + 1];
    return min(Min[l][lg], Min[r - (1 << lg) + 1][lg]);
}

ll calc(int l, int p, int r){
    if (l == 0) return x[n] - p;
    if (r == n + 1) return p - x[1];

    //2 * x - x-1 > r - x

    if (p - x[l] <= x[r] - p){
        // 2 * x[k] - x[k - 1] > x[r];

        int L = 2;
        int R = l - 1;
        int ans = 1;
        while (L <= R){
            int mid = L + R >> 1;
            if (getmax(mid, l - 1) > x[r]){
                ans = mid;
                L = mid + 1;

            }
            else R = mid - 1;
        }

        //ans = l;

        return calc(ans - 1, x[ans], r) + p - x[ans];
    }

    // 2 * x[k] - x[k + 1] <= x[l]

    int L = r + 1;
    int R = n - 1;
    int ans = n;
    while (L <= R){
        int mid = L + R >> 1;
        if (getmin(r + 1, mid) <= x[l]){
            R = mid - 1;
            ans = mid;
        }
        else L = mid + 1;
    }

    ans = r;

    return calc(l, x[ans], ans + 1) + x[ans] - p;
}

void solve(){
    cin >> n;
    for (int i = 1; i <= n; ++ i) cin >> x[i];
    prepare();

    cin >> q;
    while (q --){
        int k; cin >> k;
        int r = lower_bound(x + 1, x + n + 1, k) - x;
        int l = r - 1;
        if (x[r] == k) ++ r;
        cout << calc(l, k, r) << '\n';
    }
}

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

    return 0;
}

Compilation message

travel.cpp: In function 'long long int calc(int, int, int)':
travel.cpp:53:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   53 |             int mid = L + R >> 1;
      |                       ~~^~~
travel.cpp:73:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |         int mid = L + R >> 1;
      |                   ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 1 ms 6748 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4696 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 1 ms 6748 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4696 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 40 ms 33108 KB Output is correct
18 Correct 47 ms 44136 KB Output is correct
19 Correct 38 ms 33104 KB Output is correct
20 Correct 39 ms 33192 KB Output is correct
21 Correct 50 ms 35908 KB Output is correct
22 Incorrect 46 ms 46928 KB Output isn't correct
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4564 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Incorrect 646 ms 50332 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 1 ms 6748 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4696 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 40 ms 33108 KB Output is correct
18 Correct 47 ms 44136 KB Output is correct
19 Correct 38 ms 33104 KB Output is correct
20 Correct 39 ms 33192 KB Output is correct
21 Correct 50 ms 35908 KB Output is correct
22 Incorrect 46 ms 46928 KB Output isn't correct
23 Halted 0 ms 0 KB -