#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]=i+1;
while (l<=r){
int mid=(l+r)/2;
//cout<<l<<" "<<r<<" "<<mid<<" "<<val<<'\n';
if (get(i,mid)>=val){
st1[i][0]=mid;
l=mid+1;
}
else r=mid-1;
}
}
//cout<<st1[3][0];
st1[n][0]=n;
for1(j,1,20){
for1(i,1,n){
if (i+(1<<j)-1>n)break;
st1[i][j]=min(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]=i-1;
while (l<=r){
int mid=(l+r)/2;
if (get(mid,i)>val){
st2[i][0]=mid;
r=mid-1;
}
else l=mid+1;
}
}
for1(j,1,20){
for1(i,1,n){
if (i+(1<<j)-1>n)break;
st2[i][j]=max(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<<" "<<nw<<'\n';
if (d1<=d2){
nw=l-1;
//cout<<l<<"->"<<l-1<<'\n';
l--;
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);
}
}
b[i]+=get(pos,nw)+d1;
nw=pos;
l=nw;
}
else {
//cout<<r<<"->"<<r+1<<'\n';
r++;
nw=r;
int pos=nw;
for2(j,20,0){
if (pos+(1<<j)-1>n)continue;
if (st2[pos][j]<=l){
pos=pos+(1<<j)-1;
}
}
b[i]+=get(nw,pos)+d2;
nw=pos;
r=nw;
}
}
//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';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
668 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
736 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
668 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
736 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Incorrect |
129 ms |
39160 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
668 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
736 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |