제출 #793757

#제출 시각아이디문제언어결과실행 시간메모리
793757winter0101Bitaro's travel (JOI23_travel)C++14
5 / 100
3080 ms5580 KiB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
const int maxn=2e5+9;
long long a[maxn];
long long b[maxn];
long long c[maxn];
int st1[maxn][21];
int st2[maxn][21];
long long get(int l,int r){
if (l==r)return 0;
return c[r]-c[l];
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.ANS","w",stdout);
    int n;
    cin>>n;
    for1(i,1,n){
    cin>>a[i];
    }
    for1(i,2,n){
    c[i]=c[i-1]+a[i]-a[i-1];
    }
    for1(i,1,n){
    int l=i,r=i,nw=i;
    while (true){
        if (l==1&&r==n)break;
        if (l==1){
        b[i]+=get(nw,n);
        break;
        }
        if (r==n){
        b[i]+=get(1,nw);
        break;
        }
        long long d1=get(l-1,nw),d2=get(nw,r+1);
        //cout<<l<<" "<<r<<" "<<nw<<'\n';
        if (d1<=d2){
        b[i]+=d1;
        l--;
        nw=l;
        }
        else {
        //cout<<r<<"->"<<r+1<<'\n';
        b[i]+=d2;
        r++;
        nw=r;
        }
    }
    //cout<<b[i]<<'\n';
    }
    int q;
    cin>>q;
    for1(i,1,q){
    int x;
    cin>>x;
    if (x<=a[1])cout<<b[1]+a[1]-x<<'\n';
    else if (x>=a[n])cout<<b[n]+x-a[n]<<'\n';
    else {
    int l=1,r=n,ans=1;
    while (l<=r){
        int mid=(l+r)/2;
        if (a[mid]<=x){
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    long long d1=x-a[ans],d2=a[ans+1]-x;
    if (d1<=d2){
        cout<<d1+b[ans]<<'\n';
    }
    else cout<<d2+b[ans+1]<<'\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...