#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';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
148 ms |
37808 KB |
Output is correct |
18 |
Correct |
148 ms |
39744 KB |
Output is correct |
19 |
Correct |
155 ms |
39784 KB |
Output is correct |
20 |
Correct |
147 ms |
39784 KB |
Output is correct |
21 |
Correct |
140 ms |
39792 KB |
Output is correct |
22 |
Correct |
141 ms |
39812 KB |
Output is correct |
23 |
Correct |
138 ms |
39820 KB |
Output is correct |
24 |
Correct |
105 ms |
39744 KB |
Output is correct |
25 |
Correct |
107 ms |
39712 KB |
Output is correct |
26 |
Correct |
109 ms |
39792 KB |
Output is correct |
27 |
Correct |
120 ms |
39800 KB |
Output is correct |
28 |
Correct |
105 ms |
39756 KB |
Output is correct |
29 |
Correct |
250 ms |
39684 KB |
Output is correct |
30 |
Correct |
240 ms |
39748 KB |
Output is correct |
31 |
Correct |
247 ms |
39704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
Correct |
125 ms |
39264 KB |
Output is correct |
8 |
Correct |
119 ms |
41804 KB |
Output is correct |
9 |
Correct |
120 ms |
41832 KB |
Output is correct |
10 |
Correct |
121 ms |
41748 KB |
Output is correct |
11 |
Correct |
143 ms |
41760 KB |
Output is correct |
12 |
Correct |
120 ms |
41804 KB |
Output is correct |
13 |
Correct |
32 ms |
4136 KB |
Output is correct |
14 |
Correct |
22 ms |
1492 KB |
Output is correct |
15 |
Correct |
153 ms |
42832 KB |
Output is correct |
16 |
Correct |
141 ms |
42800 KB |
Output is correct |
17 |
Correct |
140 ms |
42828 KB |
Output is correct |
18 |
Correct |
32 ms |
4132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
148 ms |
37808 KB |
Output is correct |
18 |
Correct |
148 ms |
39744 KB |
Output is correct |
19 |
Correct |
155 ms |
39784 KB |
Output is correct |
20 |
Correct |
147 ms |
39784 KB |
Output is correct |
21 |
Correct |
140 ms |
39792 KB |
Output is correct |
22 |
Correct |
141 ms |
39812 KB |
Output is correct |
23 |
Correct |
138 ms |
39820 KB |
Output is correct |
24 |
Correct |
105 ms |
39744 KB |
Output is correct |
25 |
Correct |
107 ms |
39712 KB |
Output is correct |
26 |
Correct |
109 ms |
39792 KB |
Output is correct |
27 |
Correct |
120 ms |
39800 KB |
Output is correct |
28 |
Correct |
105 ms |
39756 KB |
Output is correct |
29 |
Correct |
250 ms |
39684 KB |
Output is correct |
30 |
Correct |
240 ms |
39748 KB |
Output is correct |
31 |
Correct |
247 ms |
39704 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
125 ms |
39264 KB |
Output is correct |
39 |
Correct |
119 ms |
41804 KB |
Output is correct |
40 |
Correct |
120 ms |
41832 KB |
Output is correct |
41 |
Correct |
121 ms |
41748 KB |
Output is correct |
42 |
Correct |
143 ms |
41760 KB |
Output is correct |
43 |
Correct |
120 ms |
41804 KB |
Output is correct |
44 |
Correct |
32 ms |
4136 KB |
Output is correct |
45 |
Correct |
22 ms |
1492 KB |
Output is correct |
46 |
Correct |
153 ms |
42832 KB |
Output is correct |
47 |
Correct |
141 ms |
42800 KB |
Output is correct |
48 |
Correct |
140 ms |
42828 KB |
Output is correct |
49 |
Correct |
32 ms |
4132 KB |
Output is correct |
50 |
Correct |
199 ms |
43916 KB |
Output is correct |
51 |
Correct |
225 ms |
43948 KB |
Output is correct |
52 |
Correct |
194 ms |
43848 KB |
Output is correct |
53 |
Correct |
191 ms |
43944 KB |
Output is correct |
54 |
Correct |
168 ms |
43916 KB |
Output is correct |
55 |
Correct |
173 ms |
43868 KB |
Output is correct |
56 |
Correct |
288 ms |
43852 KB |
Output is correct |
57 |
Correct |
269 ms |
43788 KB |
Output is correct |
58 |
Correct |
275 ms |
43776 KB |
Output is correct |