Submission #1029261

# Submission time Handle Problem Language Result Execution time Memory
1029261 2024-07-20T14:26:19 Z adaawf Bitaro's travel (JOI23_travel) C++17
100 / 100
309 ms 69132 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 = 1; 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 0 ms 344 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 0 ms 1116 KB Output is correct
16 Correct 1 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 0 ms 1116 KB Output is correct
16 Correct 1 ms 1112 KB Output is correct
17 Correct 60 ms 63068 KB Output is correct
18 Correct 58 ms 63068 KB Output is correct
19 Correct 59 ms 63056 KB Output is correct
20 Correct 59 ms 63056 KB Output is correct
21 Correct 65 ms 63056 KB Output is correct
22 Correct 64 ms 63056 KB Output is correct
23 Correct 63 ms 63056 KB Output is correct
24 Correct 63 ms 63060 KB Output is correct
25 Correct 61 ms 63056 KB Output is correct
26 Correct 67 ms 63064 KB Output is correct
27 Correct 64 ms 62984 KB Output is correct
28 Correct 67 ms 63068 KB Output is correct
29 Correct 61 ms 63064 KB Output is correct
30 Correct 59 ms 63060 KB Output is correct
31 Correct 76 ms 63060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 133 ms 64260 KB Output is correct
8 Correct 138 ms 66752 KB Output is correct
9 Correct 140 ms 66900 KB Output is correct
10 Correct 134 ms 66900 KB Output is correct
11 Correct 129 ms 66900 KB Output is correct
12 Correct 143 ms 67000 KB Output is correct
13 Correct 26 ms 4176 KB Output is correct
14 Correct 18 ms 1624 KB Output is correct
15 Correct 152 ms 67920 KB Output is correct
16 Correct 153 ms 67908 KB Output is correct
17 Correct 148 ms 67904 KB Output is correct
18 Correct 26 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 0 ms 1116 KB Output is correct
4 Correct 0 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 0 ms 1116 KB Output is correct
16 Correct 1 ms 1112 KB Output is correct
17 Correct 60 ms 63068 KB Output is correct
18 Correct 58 ms 63068 KB Output is correct
19 Correct 59 ms 63056 KB Output is correct
20 Correct 59 ms 63056 KB Output is correct
21 Correct 65 ms 63056 KB Output is correct
22 Correct 64 ms 63056 KB Output is correct
23 Correct 63 ms 63056 KB Output is correct
24 Correct 63 ms 63060 KB Output is correct
25 Correct 61 ms 63056 KB Output is correct
26 Correct 67 ms 63064 KB Output is correct
27 Correct 64 ms 62984 KB Output is correct
28 Correct 67 ms 63068 KB Output is correct
29 Correct 61 ms 63064 KB Output is correct
30 Correct 59 ms 63060 KB Output is correct
31 Correct 76 ms 63060 KB Output is correct
32 Correct 0 ms 348 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 344 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 133 ms 64260 KB Output is correct
39 Correct 138 ms 66752 KB Output is correct
40 Correct 140 ms 66900 KB Output is correct
41 Correct 134 ms 66900 KB Output is correct
42 Correct 129 ms 66900 KB Output is correct
43 Correct 143 ms 67000 KB Output is correct
44 Correct 26 ms 4176 KB Output is correct
45 Correct 18 ms 1624 KB Output is correct
46 Correct 152 ms 67920 KB Output is correct
47 Correct 153 ms 67908 KB Output is correct
48 Correct 148 ms 67904 KB Output is correct
49 Correct 26 ms 4180 KB Output is correct
50 Correct 309 ms 69008 KB Output is correct
51 Correct 274 ms 68948 KB Output is correct
52 Correct 280 ms 68948 KB Output is correct
53 Correct 212 ms 69132 KB Output is correct
54 Correct 222 ms 68948 KB Output is correct
55 Correct 219 ms 68944 KB Output is correct
56 Correct 116 ms 68932 KB Output is correct
57 Correct 114 ms 68944 KB Output is correct
58 Correct 113 ms 68932 KB Output is correct