Submission #792459

# Submission time Handle Problem Language Result Execution time Memory
792459 2023-07-25T05:10:21 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
100 / 100
1089 ms 40836 KB
#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 200200;

using namespace std;

int n;
long long inf = 1e18;
long long a[N];
long long t[N][20];
long long L[N];

long long get(int l, int r)
{
    int g = L[r - l + 1];
    return max(t[l][g], t[r - (1 << g) + 1][g]);
}

long long go_one(int &l, int&r, int &k)
{
    long long res;
    if (abs(a[k] - a[l - 1]) <= abs(a[k] - a[r + 1])) {
        l--;
        res = abs(a[k] - a[l]);
        k = l;
    } else {
        r++;
        res = abs(a[k] - a[r]);
        k = r;
    }
    return res;
}

long long solve(int k)
{
    int l = k, r = k;
    long long res = 0;
    while (l > 1 || r < n) {
        res += go_one(l, r, k);
        if (l == k) {
            int tl = 1, tr = l;
            while (tl < tr) {
                int tm = (tl + tr) / 2;
                if (get(tm, l - 1) < a[r] - a[l]) {
                    tr = tm;
                } else {
                    tl = tm + 1;
                }
            }
            res += a[l] - a[tl];
            l = k = tl;
        } else {
            int tl = r, tr = n;
            while (tl < tr) {
                int tm = (tl + tr) / 2;
                if (get(r, tm) < a[r] - a[l]) {
                    tl = tm + 1;
                } else {
                    tr = tm;
                }
            }
            res += a[tl] - a[r];
            r = k = tl;
        }
    }
    return res;
}

int main()
{
    #ifdef zxc
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    a[0] = -inf;
    a[n + 1] = inf;

    for (int i = 1; i <= n; i++) {
        t[i][0] = a[i + 1] - a[i];
    }
    for (int i = 2; i < N; i++) {
        L[i] = L[i / 2] + 1;
    }
    for (int i = 1; i < 20; i++) {
        for (int j = 1; j + (1 << i) - 1 <= n; j++) {
            t[j][i] = max(t[j][i - 1], t[j + (1 << i - 1)][i - 1]);
        }
    }

    int q;
    cin >> q;
    while (q--) {
        long long x;
        cin >> x;
        int i = lower_bound(a + 1, a + n + 1, x) - a;
        if (i > 1 && abs(a[i - 1] - x) <= abs(a[i] - x)) {
            i--;
        }

        cout << solve(i) + abs(a[i] - x) << "\n";
    }
}

Compilation message

travel.cpp: In function 'int main()':
travel.cpp:95:54: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   95 |             t[j][i] = max(t[j][i - 1], t[j + (1 << i - 1)][i - 1]);
      |                                                    ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 2132 KB Output is correct
3 Correct 1 ms 2140 KB Output is correct
4 Correct 1 ms 2132 KB Output is correct
5 Correct 2 ms 2132 KB Output is correct
6 Correct 2 ms 2132 KB Output is correct
7 Correct 1 ms 2132 KB Output is correct
8 Correct 1 ms 1876 KB Output is correct
9 Correct 2 ms 1876 KB Output is correct
10 Correct 1 ms 1876 KB Output is correct
11 Correct 1 ms 1876 KB Output is correct
12 Correct 1 ms 1876 KB Output is correct
13 Correct 2 ms 2004 KB Output is correct
14 Correct 1 ms 2132 KB Output is correct
15 Correct 1 ms 2140 KB Output is correct
16 Correct 2 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 2132 KB Output is correct
3 Correct 1 ms 2140 KB Output is correct
4 Correct 1 ms 2132 KB Output is correct
5 Correct 2 ms 2132 KB Output is correct
6 Correct 2 ms 2132 KB Output is correct
7 Correct 1 ms 2132 KB Output is correct
8 Correct 1 ms 1876 KB Output is correct
9 Correct 2 ms 1876 KB Output is correct
10 Correct 1 ms 1876 KB Output is correct
11 Correct 1 ms 1876 KB Output is correct
12 Correct 1 ms 1876 KB Output is correct
13 Correct 2 ms 2004 KB Output is correct
14 Correct 1 ms 2132 KB Output is correct
15 Correct 1 ms 2140 KB Output is correct
16 Correct 2 ms 2132 KB Output is correct
17 Correct 50 ms 35096 KB Output is correct
18 Correct 61 ms 35080 KB Output is correct
19 Correct 51 ms 35148 KB Output is correct
20 Correct 50 ms 35156 KB Output is correct
21 Correct 51 ms 35044 KB Output is correct
22 Correct 51 ms 35140 KB Output is correct
23 Correct 55 ms 35084 KB Output is correct
24 Correct 52 ms 35040 KB Output is correct
25 Correct 52 ms 35020 KB Output is correct
26 Correct 51 ms 35136 KB Output is correct
27 Correct 51 ms 35028 KB Output is correct
28 Correct 51 ms 35136 KB Output is correct
29 Correct 54 ms 35260 KB Output is correct
30 Correct 51 ms 35136 KB Output is correct
31 Correct 52 ms 35132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1864 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1876 KB Output is correct
4 Correct 1 ms 1876 KB Output is correct
5 Correct 1 ms 1876 KB Output is correct
6 Correct 2 ms 1876 KB Output is correct
7 Correct 538 ms 36824 KB Output is correct
8 Correct 508 ms 36708 KB Output is correct
9 Correct 556 ms 36752 KB Output is correct
10 Correct 529 ms 36732 KB Output is correct
11 Correct 521 ms 36684 KB Output is correct
12 Correct 515 ms 36752 KB Output is correct
13 Correct 247 ms 4420 KB Output is correct
14 Correct 220 ms 3080 KB Output is correct
15 Correct 462 ms 37152 KB Output is correct
16 Correct 456 ms 39748 KB Output is correct
17 Correct 532 ms 39760 KB Output is correct
18 Correct 230 ms 5688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 2132 KB Output is correct
3 Correct 1 ms 2140 KB Output is correct
4 Correct 1 ms 2132 KB Output is correct
5 Correct 2 ms 2132 KB Output is correct
6 Correct 2 ms 2132 KB Output is correct
7 Correct 1 ms 2132 KB Output is correct
8 Correct 1 ms 1876 KB Output is correct
9 Correct 2 ms 1876 KB Output is correct
10 Correct 1 ms 1876 KB Output is correct
11 Correct 1 ms 1876 KB Output is correct
12 Correct 1 ms 1876 KB Output is correct
13 Correct 2 ms 2004 KB Output is correct
14 Correct 1 ms 2132 KB Output is correct
15 Correct 1 ms 2140 KB Output is correct
16 Correct 2 ms 2132 KB Output is correct
17 Correct 50 ms 35096 KB Output is correct
18 Correct 61 ms 35080 KB Output is correct
19 Correct 51 ms 35148 KB Output is correct
20 Correct 50 ms 35156 KB Output is correct
21 Correct 51 ms 35044 KB Output is correct
22 Correct 51 ms 35140 KB Output is correct
23 Correct 55 ms 35084 KB Output is correct
24 Correct 52 ms 35040 KB Output is correct
25 Correct 52 ms 35020 KB Output is correct
26 Correct 51 ms 35136 KB Output is correct
27 Correct 51 ms 35028 KB Output is correct
28 Correct 51 ms 35136 KB Output is correct
29 Correct 54 ms 35260 KB Output is correct
30 Correct 51 ms 35136 KB Output is correct
31 Correct 52 ms 35132 KB Output is correct
32 Correct 1 ms 1864 KB Output is correct
33 Correct 1 ms 1868 KB Output is correct
34 Correct 1 ms 1876 KB Output is correct
35 Correct 1 ms 1876 KB Output is correct
36 Correct 1 ms 1876 KB Output is correct
37 Correct 2 ms 1876 KB Output is correct
38 Correct 538 ms 36824 KB Output is correct
39 Correct 508 ms 36708 KB Output is correct
40 Correct 556 ms 36752 KB Output is correct
41 Correct 529 ms 36732 KB Output is correct
42 Correct 521 ms 36684 KB Output is correct
43 Correct 515 ms 36752 KB Output is correct
44 Correct 247 ms 4420 KB Output is correct
45 Correct 220 ms 3080 KB Output is correct
46 Correct 462 ms 37152 KB Output is correct
47 Correct 456 ms 39748 KB Output is correct
48 Correct 532 ms 39760 KB Output is correct
49 Correct 230 ms 5688 KB Output is correct
50 Correct 1089 ms 40816 KB Output is correct
51 Correct 1048 ms 40796 KB Output is correct
52 Correct 813 ms 40784 KB Output is correct
53 Correct 852 ms 40812 KB Output is correct
54 Correct 805 ms 40836 KB Output is correct
55 Correct 734 ms 40748 KB Output is correct
56 Correct 694 ms 40672 KB Output is correct
57 Correct 682 ms 40628 KB Output is correct
58 Correct 650 ms 40620 KB Output is correct