제출 #1266766

#제출 시각아이디문제언어결과실행 시간메모리
1266766TymondBitaro's travel (JOI23_travel)C++20
0 / 100
155 ms60524 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define fi first #define se second #define vi vector<int> #define vll vector<long long> #define pii pair<int, int> #define pll pair<long long, long long> #define pb push_back #define mp make_pair #define eb emplace_back #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);} inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);} #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct pair_hash{ size_t operator()(const pair<int,int>&x)const{ return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32)); } }; const int MAXN = 2e5 + 7; const int MAXK = 18; const ll INF = 1e15; ll A[MAXN]; ll T[MAXN][MAXK]; ll P[MAXN][MAXK]; int lg[MAXN]; int n; void preprocessing(){ for(int i = 2; i < MAXN; i++){ lg[i] = lg[i / 2] + 1; } T[1][0] = INF; for(int i = 2; i <= n; i++){ T[i][0] = (ll)2 * A[i] - A[i - 1]; } P[n][0] = -INF; for(int i = 1; i < n; i++){ P[i][0] = (ll)2 * A[i] - A[i + 1]; } for(int j = 1; j < MAXK; j++){ for(int i = 1; i <= n; i++){ T[i][j] = max(T[i][j - 1], T[min(n, i + (1 << (j - 1)))][j - 1]); P[i][j] = min(P[i][j - 1], P[min(n, i + (1 << (j - 1)))][j - 1]); } } } ll mx(int l, int p){ return max(T[l][lg[p - l + 1]], T[p - (1 << lg[p - l + 1]) + 1][lg[p - l + 1]]); } ll mn(int l, int p){ return min(P[l][lg[p - l + 1]], P[p - (1 << lg[p - l + 1]) + 1][lg[p - l + 1]]); } ll solve(int ind){ int a = ind; int b = ind; ll res = 0LL; while(b - a + 1 < n){ // cout << "-----\n"; // cout << a << ' ' << b << '\n'; // cout << res << '\n'; if(a == 1){ return (ll)res + A[n] - A[a]; }else if(b == n){ return (ll)res + A[a] - A[1]; } int l = 2; int p = a; int mid; while(l < p){ mid = (l + p + 1) / 2; if(mx(mid, a) > A[b + 1]){ l = mid; }else{ p = mid - 1; } } if(l == 2 && A[2] - A[1] <= A[b + 1] - A[2]){ l--; } res = (ll)res + A[a] - A[l]; a = l; res = (ll)res + A[b + 1] - A[a]; b++; //cout << a << ' ' << b << '\n'; //cout << res << '\n'; if(a == 1){ return (ll)res + A[n] - A[b]; }else if(b == n){ return (ll)res + A[b] - A[1]; } l = b; p = n - 1; while(l < p){ mid = (l + p) / 2; if(A[a - 1] <= mn(b, mid)){ p = mid; }else{ l = mid + 1; } } if(p == n - 1 && A[n] - A[n - 1] < A[n - 1] - A[a - 1]){ p++; } res = (ll)res + A[p] - A[b]; b = p; res = (ll)res + A[b] - A[a - 1]; a--; } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; for(int i = 1; i <= n; i++){ cin >> A[i]; } preprocessing(); int Q; cin >> Q; while(Q--){ ll x; cin >> x; ll ans = 0LL; if(x <= A[1]){ ans = (ll)A[1] - x + solve(1); }else if(x >= A[n]){ ans = (ll)x - A[n] + solve(n); }else{ int l = 1; int p = n; int mid; while(l < p){ mid = (l + p + 1) / 2; if(A[mid] <= x){ l = mid; }else{ p = mid - 1; } } if(x - A[l] <= A[l + 1] - x){ ans = (ll)x - A[l] + solve(l); }else{ ans = (ll)A[l + 1] - x + solve(l + 1); } } cout << ans << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...