Submission #1029260

# Submission time Handle Problem Language Result Execution time Memory
1029260 2024-07-20T14:25:15 Z adaawf Bitaro's travel (JOI23_travel) C++17
15 / 100
139 ms 65108 KB
#include <iostream>
#include <algorithm>
using namespace std;
long long int a[200005], l[200005][19], ll[200005], la[200005][19];
long long int check(int x, int y) {
    int h = ll[y - x + 1];
    return max(l[x][h], l[y - (1 << h) + 1][h]);
}
long long int check2(int x, int y) {
    int h = ll[y - x + 1];
    return min(la[x][h], la[y - (1 << h) + 1][h]);
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 2; i <= n; i++) {
        l[i][0] = a[i] * 2 - a[i - 1];
        ll[i] = ll[i / 2] + 1;
    }
    for (int i = 1; i < n; i++) {
        la[i][0] = a[i] * 2 - a[i + 1];
    }
    l[1][0] = 1e18;
    la[n][0] = -1e18;
    for (int i = 1; i <= 18; i++) {
        for (int j = 2; j <= n - (1 << i) + 1; j++) {
            l[j][i] = max(l[j][i - 1], l[j + (1 << (i - 1))][i - 1]);
        }
        for (int j = 1; j < n - (1 << i) + 1; j++) {
            la[j][i] = min(la[j][i - 1], la[j + (1 << (i - 1))][i - 1]);
        }
    }
    int q;
    cin >> q;
    for (int jj = 0; jj < q; jj++) {
        long long int x, c = 0, l, r, flag = 0;
        cin >> x;
        if (x >= a[n]) {
            c = x - a[n];
            l = r = n;
        }
        else if (x <= a[1]) {
            c = a[1] - x;
            l = r = 1;
        }
        else {
            int h = lower_bound(a + 1, a + n + 1, x) - a;
            if (a[h] - x >= x - a[h - 1]) {
                l = r = h - 1;
                c += x - a[h - 1];
            }
            else {
                l = r = h;
                c = a[h] - x;
            }
        }
        while (l != 1 && r != n) {
            if (flag == 0) {
                int ll = 1, rr = l, res = 1;
                while (ll <= rr) {
                    int mid = (ll + rr) / 2;
                    if (check(mid, l) > a[r + 1]) {
                        res = mid;
                        ll = mid + 1;
                    }
                    else rr = mid - 1;
                }
                c += a[l] - a[res];
                l = res;
                c += a[r + 1] - a[l];
                r++;
            }
            else {
                int ll = r, rr = n, res = n;
                while (ll <= rr) {
                    int mid = (ll + rr) / 2;
                    if (check2(r, mid) <= a[l - 1]) {
                        res = mid;
                        rr = mid - 1;
                    }
                    else ll = mid + 1;
                }
                c += a[res] - a[r];
                r = res;
                c += a[r] - a[l - 1];
                l--;
            }
            //cout << l << " " << r << " " << c << '\n';
            flag ^= 1;
        }
        if (flag == 1 && r == n) c += (l != 1) * (a[n] - a[1]);
        else if (flag == 1 && l == 1) c += a[n] - a[r];
        else if (flag == 0 && r == n) c += a[l] - a[1];
        else c += a[n] - a[1];
        cout << c << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 1116 KB Output is correct
17 Correct 66 ms 64804 KB Output is correct
18 Correct 74 ms 64848 KB Output is correct
19 Correct 90 ms 64848 KB Output is correct
20 Correct 87 ms 64948 KB Output is correct
21 Correct 72 ms 65108 KB Output is correct
22 Correct 87 ms 64928 KB Output is correct
23 Correct 67 ms 64836 KB Output is correct
24 Correct 70 ms 64852 KB Output is correct
25 Correct 64 ms 64792 KB Output is correct
26 Correct 67 ms 64852 KB Output is correct
27 Correct 67 ms 64856 KB Output is correct
28 Correct 65 ms 64848 KB Output is correct
29 Correct 63 ms 64848 KB Output is correct
30 Correct 63 ms 64852 KB Output is correct
31 Correct 65 ms 64848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Incorrect 139 ms 64340 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 1116 KB Output is correct
17 Correct 66 ms 64804 KB Output is correct
18 Correct 74 ms 64848 KB Output is correct
19 Correct 90 ms 64848 KB Output is correct
20 Correct 87 ms 64948 KB Output is correct
21 Correct 72 ms 65108 KB Output is correct
22 Correct 87 ms 64928 KB Output is correct
23 Correct 67 ms 64836 KB Output is correct
24 Correct 70 ms 64852 KB Output is correct
25 Correct 64 ms 64792 KB Output is correct
26 Correct 67 ms 64852 KB Output is correct
27 Correct 67 ms 64856 KB Output is correct
28 Correct 65 ms 64848 KB Output is correct
29 Correct 63 ms 64848 KB Output is correct
30 Correct 63 ms 64852 KB Output is correct
31 Correct 65 ms 64848 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Incorrect 139 ms 64340 KB Output isn't correct
39 Halted 0 ms 0 KB -