This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 1e6 + 5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const int inf = 1e9, neg = -69420;
int n, q;
int up[mxn + 1][18], a[mxn + 1];
int get(int l, int r){
int lg = __lg(r - l + 1);
return(max(up[l][lg], up[r - (1 << lg) + 1][lg]));
}
void solve(){
int s; cin >> s;
int l = lower_bound(a + 1, a + n + 1, s) - a - 1;
if(l == 0){
cout << a[n] - s << "\n";
return;
}
int r = lower_bound(a + 1, a + n + 1, s) - a;
if(r == n || r == n + 1){
cout << s - a[1] << "\n";
return;
}
ll res = 0, pos = s;
bool dir;
if(a[r] - s < s - a[l]){
dir = 1; l = r;
}else{
dir = 0; r = l;
}
vt<pii>path;
while(l != 1 && r != n){
if(dir){
int le = r + 1, ri = n, ans = r;
while(le <= ri){
int mid = (le + ri) >> 1;
if(get(r, mid - 1) < a[r] - a[l - 1]){
ans = mid; le = mid + 1;
}else{
ri = mid - 1;
}
}
path.pb({pos, a[ans]}); path.pb({a[ans], a[l - 1]}); r = ans; l--; pos = a[l];
}else{
int le = 1, ri = l - 1, ans = l;
while(le <= ri){
int mid = (le + ri) >> 1;
if(get(mid, l - 1) <= a[r + 1] - a[l]){
ans = mid; ri = mid - 1;
}else{
le = mid + 1;
}
}
path.pb({pos, a[ans]}); path.pb({a[ans], a[r + 1]}); l = ans; r++; pos = a[r];
}
//cout << l << " " << r << "\n";
dir = !dir;
}
if(!dir){
path.pb({a[l], a[n]});
}if(dir){
path.pb({a[n], a[r]});
}
for(auto [u, v]: path){
res += abs(u - v);
}
cout << res << "\n";
/*
//cout << cnt << ' ';
if(l != 1){
// assert(dir == 0 && r == n + 1);
res += a[n] - a[1];
}if(r != n){
//assert(dir == 1 && l == 0);
res += a[n] - a[r];
}
*/
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
forr(i, 1, n + 1)cin >> a[i];
forr(i, 1, n){
up[i][0] = a[i + 1] - a[i];
}
for(int i = 1; i < 18; i++){
for(int j = 1; j + (1 << i) - 1 < n; j++){
up[j][i] = max(up[j][i - 1], up[j + (1 << (i - 1))][i - 1]);
}
}
cin >> q;
forr(i, 0, q){
solve();
}
return(0);
}
Compilation message (stderr)
travel.cpp: In function 'void solve()':
travel.cpp:87:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | for(auto [u, v]: path){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |