제출 #1266787

#제출 시각아이디문제언어결과실행 시간메모리
1266787TymondBitaro's travel (JOI23_travel)C++20
0 / 100
3096 ms59160 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; } for(int i = 1; i <= n; i++){ T[i][0] = (ll)2 * A[i] - A[i - 1]; } 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]]); } int jumpLeft(int v, ll x, int a){ int l = 1; int p = a - 1; int mid; while(l < p){ //cerr << l << ' ' << p << ' ' << x << '\n'; mid = (l + p + 1) / 2; if(mx(mid, a - 1) > x){ l = mid; }else{ p = mid - 1; } } return l; } int jumpRight(int v, ll x, int b){ int l = b + 1; int p = n; int mid; while(l < p){ //cerr << v << ' ' << l << ' ' << p << ' ' << x << '\n'; mid = (l + p) / 2; if(x <= mn(b + 1, mid)){ p = mid; }else{ l = mid + 1; } } return l; } ll solve(int ind){ int a = ind; int b = ind; int cur = ind; ll res = 0LL; while(b - a + 1 < n){ if(A[cur] - A[a - 1] <= A[b + 1] - A[cur]){ a = jumpLeft(cur, A[b + 1], a); res = (ll)res + (ll)A[cur] - A[a]; cur = a; }else{ b = jumpRight(cur, A[a - 1], b); res = (ll)res + (ll)A[b] - A[cur]; cur = b; } } 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]; } A[0] = -INF; A[n + 1] = INF; preprocessing(); int Q; cin >> Q; while(Q--){ ll x; cin >> x; ll ans = 0LL; if(x <= A[1]){ ans = (ll)A[n] - x; }else if(x >= A[n]){ ans = (ll)x - A[1]; }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...