답안 #793159

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793159 2023-07-25T14:39:05 Z phoenix Bitaro's travel (JOI23_travel) C++17
100 / 100
710 ms 61884 KB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const ll INF = 1e18;
const int N = 2e5 + 10;

template<typename T>
struct SparseTable {
    int len;
    vector<vector<T>> st;
    T (*F) (T, T);
    void init(vector<T> &a, T (*f)(T, T)) {
        len = (int)a.size();
        F = f;
        int mxpw = 32 - __builtin_clz(len);
        st.resize(mxpw);
        st[0] = a;  
        for(int k = 1; k < mxpw; k++) {
            st[k].resize(len - (1 << k) + 1);
            for(int i = 0; i < (int)st[k].size(); i++) 
                st[k][i] = F(st[k - 1][i], st[k - 1][i + (1 << k - 1)]);
        }
    }
    T get(int l, int r) {
        assert(l >= 0 && r < len);
        int mxpw = 31 - __builtin_clz(r - l + 1);
        return F(st[mxpw][l], st[mxpw][r - (1 << mxpw) + 1]);
    }
};

SparseTable<ll> st_ld, st_rd;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n;
    cin >> n;
    vector<ll> x(n + 2);
    x[0] = -INF; x[n + 1] = INF;

    for(int i = 1; i <= n; i++)
        cin >> x[i];
    vector<ll> b(n + 1);
    for(int i = 1; i <= n; i++) 
        b[i] = 2 * x[i + 1] - x[i];
    st_ld.init(b, [](ll a, ll b) {return (a > b ? a : b);});
    for(int i = 1; i <= n; i++) {
        b[i] = x[i] - 2 * x[i - 1];
    }
    st_rd.init(b, [](ll x, ll y) {return (x > y ? x : y);});
    int q;
    cin >> q;
    while(q --> 0) {
        int st;
        cin >> st;
        if(st <= x[1]) {
            cout << x[n] - st << '\n';
            continue;
        }
        if(st >= x[n]) {
            cout << st - x[1] << '\n';
            continue;
        }
        int lb, rb, bin_lb, bin_rb;
        bool turn = 0;
        bin_lb = 0, bin_rb = n + 1;
        while(bin_rb - bin_lb > 1) {
            int mid = (bin_rb + bin_lb) / 2;
            if(x[mid] < st) bin_lb = mid;
            else bin_rb = mid;
        }
        lb = bin_lb;
        bin_lb = 0, bin_rb = n + 1;
        while(bin_rb - bin_lb > 1) {
            int mid = (bin_rb + bin_lb) / 2;
            if(x[mid] > st) bin_rb = mid;
            else bin_lb = mid;
        }
        ll sum = 0;
        rb = bin_rb;
        sum += min(st - x[lb], x[rb] - st);
        if(st - x[lb] <= x[rb] - st) rb--;
        else lb++, turn = 1;
        // cout << lb << ' ' << rb << ' ' << sum << ' ' << turn << '\n';
        while(lb > 1 || rb < n) {
            if(!turn) {
                bin_lb = 0, bin_rb = lb;
                while(bin_rb - bin_lb > 1) {
                    int mid = (bin_lb + bin_rb) / 2;
                    if(st_ld.get(mid, lb - 1) <= x[rb + 1]) bin_rb = mid;
                    else bin_lb = mid;
                }
                sum += x[lb] - x[bin_rb];
                lb = bin_rb;
                if(rb < n) sum += x[++rb] - x[lb];
            } else {
                bin_lb = rb; bin_rb = n + 1;
                while(bin_rb - bin_lb > 1) {
                    int mid = (bin_rb + bin_lb) / 2;
                    if(st_rd.get(rb + 1, mid) < -x[lb - 1]) bin_lb = mid;
                    else bin_rb = mid;
                }
                sum += x[bin_lb] - x[rb];
                rb = bin_lb;
                if(lb > 1) sum += x[rb] - x[--lb];
            }
            // cout << lb << ' ' << rb << ' ' << sum << ' ' << turn << '\n';
            turn = !turn;
        }
        cout << sum << '\n';
    }
}

Compilation message

travel.cpp: In instantiation of 'void SparseTable<T>::init(std::vector<_Tp>&, T (*)(T, T)) [with T = long long int]':
travel.cpp:48:59:   required from here
travel.cpp:23:66: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   23 |                 st[k][i] = F(st[k - 1][i], st[k - 1][i + (1 << k - 1)]);
      |                                                                ~~^~~
# 결과 실행 시간 메모리 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 608 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 316 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
# 결과 실행 시간 메모리 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 608 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 316 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 48 ms 57652 KB Output is correct
18 Correct 48 ms 57672 KB Output is correct
19 Correct 47 ms 57676 KB Output is correct
20 Correct 47 ms 57796 KB Output is correct
21 Correct 48 ms 57672 KB Output is correct
22 Correct 47 ms 57676 KB Output is correct
23 Correct 47 ms 57604 KB Output is correct
24 Correct 48 ms 57684 KB Output is correct
25 Correct 48 ms 57568 KB Output is correct
26 Correct 57 ms 57676 KB Output is correct
27 Correct 49 ms 57688 KB Output is correct
28 Correct 48 ms 57664 KB Output is correct
29 Correct 48 ms 57632 KB Output is correct
30 Correct 48 ms 57536 KB Output is correct
31 Correct 47 ms 57564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 248 ms 58072 KB Output is correct
8 Correct 210 ms 59692 KB Output is correct
9 Correct 205 ms 59588 KB Output is correct
10 Correct 227 ms 59720 KB Output is correct
11 Correct 206 ms 59704 KB Output is correct
12 Correct 200 ms 59612 KB Output is correct
13 Correct 30 ms 4172 KB Output is correct
14 Correct 26 ms 1484 KB Output is correct
15 Correct 275 ms 60748 KB Output is correct
16 Correct 298 ms 60744 KB Output is correct
17 Correct 272 ms 60876 KB Output is correct
18 Correct 30 ms 4060 KB Output is correct
# 결과 실행 시간 메모리 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 608 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 316 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 48 ms 57652 KB Output is correct
18 Correct 48 ms 57672 KB Output is correct
19 Correct 47 ms 57676 KB Output is correct
20 Correct 47 ms 57796 KB Output is correct
21 Correct 48 ms 57672 KB Output is correct
22 Correct 47 ms 57676 KB Output is correct
23 Correct 47 ms 57604 KB Output is correct
24 Correct 48 ms 57684 KB Output is correct
25 Correct 48 ms 57568 KB Output is correct
26 Correct 57 ms 57676 KB Output is correct
27 Correct 49 ms 57688 KB Output is correct
28 Correct 48 ms 57664 KB Output is correct
29 Correct 48 ms 57632 KB Output is correct
30 Correct 48 ms 57536 KB Output is correct
31 Correct 47 ms 57564 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 248 ms 58072 KB Output is correct
39 Correct 210 ms 59692 KB Output is correct
40 Correct 205 ms 59588 KB Output is correct
41 Correct 227 ms 59720 KB Output is correct
42 Correct 206 ms 59704 KB Output is correct
43 Correct 200 ms 59612 KB Output is correct
44 Correct 30 ms 4172 KB Output is correct
45 Correct 26 ms 1484 KB Output is correct
46 Correct 275 ms 60748 KB Output is correct
47 Correct 298 ms 60744 KB Output is correct
48 Correct 272 ms 60876 KB Output is correct
49 Correct 30 ms 4060 KB Output is correct
50 Correct 710 ms 61852 KB Output is correct
51 Correct 606 ms 61816 KB Output is correct
52 Correct 493 ms 61776 KB Output is correct
53 Correct 541 ms 61784 KB Output is correct
54 Correct 506 ms 61792 KB Output is correct
55 Correct 513 ms 61884 KB Output is correct
56 Correct 192 ms 61640 KB Output is correct
57 Correct 195 ms 61712 KB Output is correct
58 Correct 189 ms 61624 KB Output is correct