Submission #1330141

#TimeUsernameProblemLanguageResultExecution timeMemory
1330141anteknneBitaro's travel (JOI23_travel)C++20
100 / 100
281 ms35496 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const int MAXN = 200 * 1000 + 17;
const int MAXLOG = 20;
int a[MAXN];
int st1[MAXN][MAXLOG];
int st2[MAXN][MAXLOG];
int tlog[MAXN];

int odczytaj1 (int l, int r) {
    int x = tlog[r - l + 1];
    return max(st1[l][x], st1[r - (1 << x) + 1][x]);
}

int odczytaj2 (int l, int r) {
    int x = tlog[r - l + 1];
    return max(st2[l][x], st2[r - (1 << x) + 1][x]);
}

int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

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

    for (int i = 0; i < n - 1; i ++) {
        st1[i][0] = a[i + 1] - 2 * a[i];
        st2[i][0] = 2 * a[i + 1] - a[i];
    }

    for (int i = 1; i <= n; i ++) {
        tlog[i] = log2(i);
    }

    int m = n - 1;
    for (int j = 1; j < MAXLOG; j ++) {
        for (int i = 0; i + (1 << j) <= m; i ++) {
            st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]);
            st2[i][j] = max(st2[i][j - 1], st2[i + (1 << (j - 1))][j - 1]);
        }
    }

    int q;
    cin >> q;

    int s;
    ll wyn = 0;
    while (q --) {
        cin >> s;

        if (n == 1) {
            cout << abs(s - a[0]) << "\n";
            continue;
        }

        auto it = lower_bound(a, a + n, s);

        if (it != a) {
            it --;
        }
        int ind = it - a;
        if (ind == (n - 1)) {
            ind --;
        }

        int l, r;
        bool lewo;
        wyn = 0;
        if (abs(s - a[ind]) <= abs(a[ind + 1] - s)) {
            lewo = false;
            wyn = abs(s - a[ind]);
            l = ind;
            r = ind;
        } else {
            lewo = true;
            wyn = abs(a[ind + 1] - s);
            l = ind + 1;
            r = ind + 1;
            ind ++;
        }

        while (l != 0 || r != n - 1) {
            if (l == 0) {
                wyn += ll(a[n - 1] - a[ind]);
                break;
            }
            if (r == (n - 1)) {
                wyn += ll(a[ind] - a[0]);
                break;
            }
            if (!lewo) {
                int p = r, k = n - 2, w = n - 1;
                while (p <= k) {
                    int sr = (p + k)/ 2;
                    if (odczytaj1(r, sr) >= -a[l - 1]) {
                        w = sr;
                        k = sr - 1;
                    } else {
                        p = sr + 1;
                    }
                }
                wyn += ll(a[w] - a[r]);
                r = w;
                wyn += ll(a[r] - a[l - 1]);
                l --;
                ind = l;
            } else {
                int p = 0, k = l - 1, w = 0;
                while (p <= k) {
                    int sr = (p + k)/ 2;
                    if (odczytaj2(sr, l - 1) > a[r + 1]) {
                        w = sr + 1;
                        p = sr + 1;
                    } else {
                        k = sr - 1;
                    }
                }
                wyn += ll(a[l] - a[w]);
                l = w;
                wyn += ll(a[r + 1] - a[l]);
                r ++;
                ind = r;
            }

            lewo ^= 1;
        }

        cout << wyn << "\n";

    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...