Submission #741560

# Submission time Handle Problem Language Result Execution time Memory
741560 2023-05-14T10:37:06 Z stevancv Bitaro's travel (JOI23_travel) C++14
100 / 100
310 ms 36116 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const ll linf = 1e18;
int l[N][18], r[N][18], logs[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n; cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) l[i][0] = 2 * a[i] - a[i - 1];
    for (int j = 1; j < 18; j++) {
        for (int i = 1; i < n - (1 << j) + 1; i++) {
            l[i][j] = max(l[i][j - 1], l[i + (1 << (j - 1))][j - 1]);
        }
    }
    for (int i = 0; i < n - 1; i++) r[i][0] = 2 * a[i] - a[i + 1];
    for (int j = 1; j < 18; j++) {
        for (int i = 0; i < n - (1 << j); i++) {
            r[i][j] = min(r[i][j - 1], r[i + (1 << (j - 1))][j - 1]);
        }
    }
    int zz = 0;
    for (int i = 1; i <= n; i++) {
        if ((1 << (zz + 1)) == i) zz += 1;
        logs[i] = zz;
    }
    auto GetLeft = [&] (int p, int q) {
        if (p > q) return 0;
        int o = logs[q - p + 1];
        return max(l[p][o], l[q - (1 << o) + 1][o]);
    };
    auto GetRight = [&] (int p, int q) {
        if (p > q) return 0;
        int o = logs[q - p + 1];
        return min(r[p][o], r[q - (1 << o) + 1][o]);
    };
    int q; cin >> q;
    while (q--) {
        int s; cin >> s;
        if (s >= a[n - 1]) {
            cout << s - a[0] << en;
            continue;
        }
        if (s <= a[0]) {
            cout << a[n - 1] - s << en;
            continue;
        }
        int up = (int)(upper_bound(a.begin(), a.end(), s) - a.begin());
        int lo = up - 1;
        if (a[lo] == s) lo -= 1;
        int uu = 0;
        ll ans = s;
        if (s - a[lo] > a[up] - s) {
            uu = 1;
            ans = -s;
        }
        while (1) {
            if (uu == 0) {
                if (GetLeft(1, lo) <= a[up] || lo == 0) {
                    ans += a[n - 1] - 2 * a[0];
                    cout << ans << en;
                    break;
                }
                int lef = 1, rig = lo, gde = -1;
                while (lef <= rig) {
                    int mid = lef + rig >> 1;
                    if (GetLeft(mid, lo) > a[up]) {
                        lef = mid + 1;
                        gde = mid;
                    }
                    else rig = mid - 1;
                }
                assert(gde != -1);
                ans -= 2 * a[gde];
                uu ^= 1;
                lo = gde - 1;
                continue;
            }
            if (GetRight(up, n - 2) > a[lo] || up == n - 1) {
                ans += 2 * a[n - 1] - a[0];
                cout << ans << en;
                break;
            }
            int lef = up, rig = n - 2, gde = -1;
            while (lef <= rig) {
                int mid = lef + rig >> 1;
                if (GetRight(up, mid) <= a[lo]) {
                    rig = mid - 1;
                    gde = mid;
                }
                else lef = mid + 1;
            }
            assert(gde != -1);
            ans += 2 * a[gde];
            uu ^= 1;
            up = gde + 1;
        }
    }
    return 0;
}

Compilation message

travel.cpp: In function 'int main()':
travel.cpp:75:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |                     int mid = lef + rig >> 1;
      |                               ~~~~^~~~~
travel.cpp:95:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   95 |                 int mid = lef + rig >> 1;
      |                           ~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 76 ms 31316 KB Output is correct
18 Correct 71 ms 31116 KB Output is correct
19 Correct 74 ms 31204 KB Output is correct
20 Correct 75 ms 31196 KB Output is correct
21 Correct 79 ms 31268 KB Output is correct
22 Correct 79 ms 31416 KB Output is correct
23 Correct 68 ms 31060 KB Output is correct
24 Correct 77 ms 31012 KB Output is correct
25 Correct 74 ms 31080 KB Output is correct
26 Correct 75 ms 31720 KB Output is correct
27 Correct 68 ms 31564 KB Output is correct
28 Correct 70 ms 31644 KB Output is correct
29 Correct 77 ms 31304 KB Output is correct
30 Correct 70 ms 31436 KB Output is correct
31 Correct 77 ms 31572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 132 ms 33696 KB Output is correct
8 Correct 162 ms 32844 KB Output is correct
9 Correct 141 ms 32676 KB Output is correct
10 Correct 148 ms 32848 KB Output is correct
11 Correct 134 ms 32996 KB Output is correct
12 Correct 149 ms 32868 KB Output is correct
13 Correct 41 ms 3280 KB Output is correct
14 Correct 27 ms 1316 KB Output is correct
15 Correct 152 ms 33820 KB Output is correct
16 Correct 152 ms 34444 KB Output is correct
17 Correct 158 ms 34848 KB Output is correct
18 Correct 42 ms 3792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 76 ms 31316 KB Output is correct
18 Correct 71 ms 31116 KB Output is correct
19 Correct 74 ms 31204 KB Output is correct
20 Correct 75 ms 31196 KB Output is correct
21 Correct 79 ms 31268 KB Output is correct
22 Correct 79 ms 31416 KB Output is correct
23 Correct 68 ms 31060 KB Output is correct
24 Correct 77 ms 31012 KB Output is correct
25 Correct 74 ms 31080 KB Output is correct
26 Correct 75 ms 31720 KB Output is correct
27 Correct 68 ms 31564 KB Output is correct
28 Correct 70 ms 31644 KB Output is correct
29 Correct 77 ms 31304 KB Output is correct
30 Correct 70 ms 31436 KB Output is correct
31 Correct 77 ms 31572 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 132 ms 33696 KB Output is correct
39 Correct 162 ms 32844 KB Output is correct
40 Correct 141 ms 32676 KB Output is correct
41 Correct 148 ms 32848 KB Output is correct
42 Correct 134 ms 32996 KB Output is correct
43 Correct 149 ms 32868 KB Output is correct
44 Correct 41 ms 3280 KB Output is correct
45 Correct 27 ms 1316 KB Output is correct
46 Correct 152 ms 33820 KB Output is correct
47 Correct 152 ms 34444 KB Output is correct
48 Correct 158 ms 34848 KB Output is correct
49 Correct 42 ms 3792 KB Output is correct
50 Correct 309 ms 36116 KB Output is correct
51 Correct 300 ms 35404 KB Output is correct
52 Correct 310 ms 35988 KB Output is correct
53 Correct 214 ms 35736 KB Output is correct
54 Correct 205 ms 36080 KB Output is correct
55 Correct 207 ms 35548 KB Output is correct
56 Correct 127 ms 35352 KB Output is correct
57 Correct 127 ms 35700 KB Output is correct
58 Correct 124 ms 35660 KB Output is correct