Submission #890919

#TimeUsernameProblemLanguageResultExecution timeMemory
890919vjudge1Bitaro's travel (JOI23_travel)C++17
100 / 100
344 ms75356 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define ar array #define pb push_back #define ln '\n' #define int long long using i64 = long long; template <class F, class _S> bool chmin(F &u, const _S &v){ bool flag = false; if ( u > v ){ u = v; flag |= true; } return flag; } template <class F, class _S> bool chmax(F &u, const _S &v){ bool flag = false; if ( u < v ){ u = v; flag |= true; } return flag; } struct Sp{ vector <vector<int>> T; int _s; Sp(auto &a, int s){ _s = s; T.resize(20); int n = a.size(); for ( auto &x: T ){ x.resize(n); } T[0] = a; for ( auto &u: T[0] ) u *= _s; for ( int i = 1; i < 20; i++ ){ int k = 1 << i - 1; for ( int j = 0; j < n; j++ ){ T[i][j] = min(T[i - 1][j], T[i - 1][min(n - 1, j + k)]); } } } int get(int l, int r){ int lg = __lg(r - l + 1), k = 1 << lg; return min(T[lg][l], T[lg][r - k + 1]) * _s; } }; signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector <int> X(n); for ( auto &u: X ) cin >> u; int q; cin >> q; vector <int> S(q); for ( auto &u: S ) cin >> u; vector <int> df(n - 1), df2(n - 1); for ( int i = 0; i + 1 < n; i++ ){ df[i] = X[i + 1] * 2 - X[i]; df2[i] = X[i + 1] - X[i] * 2; } Sp sp(df, -1), ap(df2, -1); for ( auto &xx: S ){ int it = lower_bound(all(X), xx) - X.begin(); if ( it - 1 >= 0 && (it == n || X[it] - xx >= xx - X[it - 1]) ) --it; int ans = abs(X[it] - xx), l = it - 1, r = it + 1, x = X[it]; while ( l >= 0 || r < n ){ // cout << l + 1 << " " << r + 1 << " -> " << x << ", " << ans << ln; if ( l < 0 ){ ans += abs(X.back() - x); break; } else if ( r >= n ){ ans += abs(X[0] - x); break; } else{ if ( abs(X[l] - x) <= abs(X[r] - x) ){ int tl = 0, tr = l; while ( tl + 1 < tr ){ int md = (tl + tr) >> 1; if ( sp.get(md, l - 1) - X[l] <= abs(X[r] - x) ){ tr = md; } else tl = md; } ans += abs(X[tr] - x); x = X[tr--]; l = tr; } else{ int tl = r - 1, tr = n - 1; while ( tl + 1 < tr ){ int md = (tl + tr) >> 1; if ( ap.get(r, md) + X[r] < abs(X[l] - x) ){ tl = md; } else tr = md; } tl++; ans += abs(X[tr] - x); x = X[tr++]; r = tr; } } } cout << ans << ln; } cout << '\n'; }

Compilation message (stderr)

travel.cpp:34:8: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   34 |     Sp(auto &a, int s){
      |        ^~~~
travel.cpp: In instantiation of 'Sp::Sp(auto:23&, long long int) [with auto:23 = std::vector<long long int>]':
travel.cpp:71:17:   required from here
travel.cpp:44:28: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   44 |             int k = 1 << i - 1;
      |                          ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...