Submission #1298048

#TimeUsernameProblemLanguageResultExecution timeMemory
1298048trandangquangBitaro's travel (JOI23_travel)C++20
100 / 100
341 ms4564 KiB
#include<bits/stdc++.h>
using namespace std;

#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
#define _ << " " <<

template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}

const int N=2e5+5;

int n,q,x[N];

void solve(){
    cin>>n;
    foru(i,1,n) cin>>x[i];

    cin>>q;
    foru(i,1,q){
        int s; cin>>s;
        int p=lower_bound(x+1,x+1+n,s)-x;
        if(p==1) p=1;
        else if(p==n+1) p=n;
        else{
            if(s-x[p-1] <= x[p]-s) --p;
        }

        ll res=abs(x[p]-s);
        int l=p, r=p;
        while(l>1 && r<n){
            if(l==p){
                int dist=x[r+1]-x[p];
                int nxt=lower_bound(x+1,x+1+n,x[p]-dist)-x;
                if(nxt<l){
                    res+=x[p]-x[nxt];
                    p=l=nxt;
                }else{
                    res+=dist;
                    ++r; p=r;
                }
            } else{
                int dist=x[p]-x[l-1];
                int nxt=lower_bound(x+1,x+1+n,x[p]+dist)-x-1;
                if(nxt>r){
                    res+=x[nxt]-x[p];
                    p=r=nxt;
                } else{
                    res+=dist;
                    --l; p=l;
                }
            }
        }
        res+=x[n]-x[1];

        cout<<res<<'\n';
    }
}

int32_t main(){
    #define task "test"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    int tc=1; //cin>>tc;
    foru(i,1,tc){
        solve();
    }
}

Compilation message (stderr)

travel.cpp: In function 'int32_t main()':
travel.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...