Submission #916840

# Submission time Handle Problem Language Result Execution time Memory
916840 2024-01-26T15:15:38 Z thangdz2k7 Bitaro's travel (JOI23_travel) C++17
15 / 100
3000 ms 35456 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;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 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 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 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 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 43 ms 35112 KB Output is correct
18 Correct 40 ms 35156 KB Output is correct
19 Correct 38 ms 35156 KB Output is correct
20 Correct 38 ms 35244 KB Output is correct
21 Correct 39 ms 35456 KB Output is correct
22 Correct 44 ms 35156 KB Output is correct
23 Correct 40 ms 35156 KB Output is correct
24 Correct 41 ms 35152 KB Output is correct
25 Correct 41 ms 35208 KB Output is correct
26 Correct 42 ms 35160 KB Output is correct
27 Correct 38 ms 35156 KB Output is correct
28 Correct 39 ms 35164 KB Output is correct
29 Correct 38 ms 35156 KB Output is correct
30 Correct 39 ms 35064 KB Output is correct
31 Correct 40 ms 35156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Execution timed out 3071 ms 33640 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6488 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 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 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 43 ms 35112 KB Output is correct
18 Correct 40 ms 35156 KB Output is correct
19 Correct 38 ms 35156 KB Output is correct
20 Correct 38 ms 35244 KB Output is correct
21 Correct 39 ms 35456 KB Output is correct
22 Correct 44 ms 35156 KB Output is correct
23 Correct 40 ms 35156 KB Output is correct
24 Correct 41 ms 35152 KB Output is correct
25 Correct 41 ms 35208 KB Output is correct
26 Correct 42 ms 35160 KB Output is correct
27 Correct 38 ms 35156 KB Output is correct
28 Correct 39 ms 35164 KB Output is correct
29 Correct 38 ms 35156 KB Output is correct
30 Correct 39 ms 35064 KB Output is correct
31 Correct 40 ms 35156 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Execution timed out 3071 ms 33640 KB Time limit exceeded
39 Halted 0 ms 0 KB -