Submission #793847

#TimeUsernameProblemLanguageResultExecution timeMemory
793847winter0101Bitaro's travel (JOI23_travel)C++14
100 / 100
288 ms43948 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.OUT","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-1){ //2*val[i]>sum-val[i] long long val=(a[i+1]-a[i])*2; int l=i+2,r=n; st1[i][0]=n+1; while (l<=r){ int mid=(l+r)/2; //cout<<l<<" "<<r<<" "<<mid<<" "<<val<<'\n'; if (get(i,mid)>=val){ st1[i][0]=mid; r=mid-1; } else l=mid+1; } } //cout<<st1[1][0]<<'\n'; st1[n+1][0]=n+1; st1[n][0]=n; for1(j,1,20){ for1(i,1,n){ if (i+(1<<j)-1>n)break; st1[i][j]=max(st1[i][j-1],st1[i+(1<<(j-1))][j-1]); } } st2[1][0]=1; for1(i,2,n){ long long val=(a[i]-a[i-1])*2; int l=1,r=i-2; st2[i][0]=0; while (l<=r){ int mid=(l+r)/2; if (get(mid,i)>val){ st2[i][0]=mid; l=mid+1; } else r=mid-1; } } //cout<<st2[5][0]; for1(j,1,20){ for1(i,1,n){ if (i+(1<<j)-1>n)break; st2[i][j]=min(st2[i][j-1],st2[i+(1<<(j-1))][j-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<<'\n'; if (d1<=d2){ nw=l-1; //cout<<l<<"->"<<l-1<<'\n'; l--; if (l==1){ b[i]+=d1; continue; } int pos=nw; for2(j,20,0){ if (pos-(1<<j)+1<=0)continue; if (st1[pos-(1<<j)+1][j]<=r+1){ pos=(pos-(1<<j)+1); } } //cerr<<pos<<" "<<nw<<'\n'; //if (pos>nw)cout<<"v"; b[i]+=get(pos,nw)+d1; nw=pos; l=nw; } else { //cout<<r<<"->"<<r+1<<'\n'; r++; nw=r; if (r==n){ b[i]+=d2; continue; } int pos=nw; for2(j,20,0){ if (pos+(1<<j)-1>n)continue; if (st2[pos][j]>=l-1){ pos=pos+(1<<j)-1; } } b[i]+=get(nw,pos)+d2; //cout<<nw<<" "<<pos<<'\n'; nw=pos; r=nw; } } //cout<<b[i]<<'\n'; } int q; cin>>q; for1(i,1,q){ int x; cin>>x; //continue; 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...