Submission #878333

# Submission time Handle Problem Language Result Execution time Memory
878333 2023-11-24T08:21:13 Z LucaIlie Bitaro's travel (JOI23_travel) C++17
100 / 100
411 ms 11308 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5;
const long long INF = 1e9 + 5;
int leftP[MAX_N + 2], rightP[MAX_N + 2];
long long x[MAX_N + 2], ans[MAX_N + 2];
int n;

int solve( int i ) {
    if ( ans[i] > 0 )
        return ans[i];

    if ( i == 1 )
        ans[i] = x[n] - x[1];
    else if ( i == n )
        ans[i] = x[n] - x[1];
    else if ( i == leftP[i] )
        ans[i] = x[rightP[i]] - x[i] + solve( rightP[i] );
    else
        ans[i] = x[i] - x[leftP[i]] + solve( leftP[i] );
    return ans[i];
}

int main() {
    cin >> n;
    for ( int i = 1; i <= n; i++ )
        cin >> x[i];
    x[0] = -INF;
    x[n + 1] = 3 * INF;

    vector<int> stLeft;
    stLeft.push_back( 0 );
    for ( int i = 1; i <= n; i++ ) {
        int l = 0, r = stLeft.size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;
            int p = stLeft[mid];
            if ( x[p + 1] - x[p] <= x[i + 1] - x[p + 1] )
                r = mid;
            else
                l = mid;
        }
        leftP[i] = stLeft[l] + 1;
        while ( !stLeft.empty() && 2 * x[i + 1] - x[i] >= 2 * x[stLeft.back() + 1] - x[stLeft.back()] )
            stLeft.pop_back();
        stLeft.push_back( i );
    }

    vector<int> stRight;
    stRight.push_back( n + 1 );
    for ( int i = n; i >= 1; i-- ) {
        int l = 0, r = stRight.size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;
            int p = stRight[mid];
            if ( x[p] - x[p - 1] < x[p - 1] - x[i - 1] )
                r = mid;
            else
                l = mid;
        }
        rightP[i] = stRight[l] - 1;
        while ( !stRight.empty() && 2 * x[i - 1] - x[i] <= 2 * x[stRight.back() - 1] - x[stRight.back()] )
            stRight.pop_back();
        stRight.push_back( i );
    }

    for ( int i = 1; i <= n; i++ )
        ans[i] = solve( i );
    
    int q;
    cin >> q;
    while ( q-- ) {
        int y;
        cin >> y;
        int l = 0, r = n + 1;
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;
            if ( y >= x[mid] )
                l = mid;
            else
                r = mid;
        }

        if ( y - x[l] <= x[l + 1] - y )
            cout << y - x[l] + ans[l] << "\n";
        else
            cout << x[l + 1] - y + ans[l + 1] << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4556 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 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 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4556 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 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 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 61 ms 6996 KB Output is correct
18 Correct 74 ms 6996 KB Output is correct
19 Correct 61 ms 6816 KB Output is correct
20 Correct 62 ms 6936 KB Output is correct
21 Correct 76 ms 6808 KB Output is correct
22 Correct 63 ms 6992 KB Output is correct
23 Correct 62 ms 6820 KB Output is correct
24 Correct 62 ms 6808 KB Output is correct
25 Correct 62 ms 6992 KB Output is correct
26 Correct 78 ms 6948 KB Output is correct
27 Correct 61 ms 6996 KB Output is correct
28 Correct 63 ms 6996 KB Output is correct
29 Correct 64 ms 6988 KB Output is correct
30 Correct 65 ms 6936 KB Output is correct
31 Correct 66 ms 6816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4540 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 Correct 372 ms 9068 KB Output is correct
8 Correct 390 ms 8880 KB Output is correct
9 Correct 373 ms 8972 KB Output is correct
10 Correct 365 ms 9032 KB Output is correct
11 Correct 395 ms 8868 KB Output is correct
12 Correct 399 ms 9268 KB Output is correct
13 Correct 297 ms 8192 KB Output is correct
14 Correct 268 ms 5556 KB Output is correct
15 Correct 369 ms 10064 KB Output is correct
16 Correct 401 ms 10080 KB Output is correct
17 Correct 372 ms 10064 KB Output is correct
18 Correct 308 ms 8348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4556 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4440 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 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 61 ms 6996 KB Output is correct
18 Correct 74 ms 6996 KB Output is correct
19 Correct 61 ms 6816 KB Output is correct
20 Correct 62 ms 6936 KB Output is correct
21 Correct 76 ms 6808 KB Output is correct
22 Correct 63 ms 6992 KB Output is correct
23 Correct 62 ms 6820 KB Output is correct
24 Correct 62 ms 6808 KB Output is correct
25 Correct 62 ms 6992 KB Output is correct
26 Correct 78 ms 6948 KB Output is correct
27 Correct 61 ms 6996 KB Output is correct
28 Correct 63 ms 6996 KB Output is correct
29 Correct 64 ms 6988 KB Output is correct
30 Correct 65 ms 6936 KB Output is correct
31 Correct 66 ms 6816 KB Output is correct
32 Correct 1 ms 4540 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 Correct 372 ms 9068 KB Output is correct
39 Correct 390 ms 8880 KB Output is correct
40 Correct 373 ms 8972 KB Output is correct
41 Correct 365 ms 9032 KB Output is correct
42 Correct 395 ms 8868 KB Output is correct
43 Correct 399 ms 9268 KB Output is correct
44 Correct 297 ms 8192 KB Output is correct
45 Correct 268 ms 5556 KB Output is correct
46 Correct 369 ms 10064 KB Output is correct
47 Correct 401 ms 10080 KB Output is correct
48 Correct 372 ms 10064 KB Output is correct
49 Correct 308 ms 8348 KB Output is correct
50 Correct 390 ms 11172 KB Output is correct
51 Correct 404 ms 11108 KB Output is correct
52 Correct 389 ms 11024 KB Output is correct
53 Correct 411 ms 11308 KB Output is correct
54 Correct 397 ms 11088 KB Output is correct
55 Correct 405 ms 10992 KB Output is correct
56 Correct 379 ms 10836 KB Output is correct
57 Correct 376 ms 10836 KB Output is correct
58 Correct 373 ms 11164 KB Output is correct