#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e17;
ll n,v[200005];
ll lft[200005],rgt[200005],rmqlft[21][200005],rmqrgt[21][200005],loga[200005];
bool finish(int l,int r)
{
if(l==0&&r>=n)
return 1;
if(l<=1&&r==n+1)
return 1;
return 0;
}
ll getlft(ll l,ll r)
{
int lg=loga[r-l+1];
return max(rmqlft[lg][l],rmqlft[lg][r-(1<<lg)+1]);
}
ll getrgt(ll l,ll r)
{
int lg=loga[r-l+1];
return max(rmqrgt[lg][l],rmqrgt[lg][r-(1<<lg)+1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i];
for(int i=1;i<=n;i++)
{
if(i==1)
{
lft[i]=INF;
continue;
}
lft[i]=(v[i]-v[i-1])-(v[n]-v[i]);
}
for(int i=n;i>=1;i--)
{
if(i==n)
{
rgt[i]=INF;
continue;
}
rgt[i]=(v[i+1]-v[i])-(v[i]-v[1]);
}
for(int i=1;i<=n;i++)
{
rmqlft[0][i]=lft[i];
rmqrgt[0][i]=rgt[i];
for(int bit=0;bit<=20;bit++)
if((1<<bit)<=i)
loga[i]=bit;
}
for(int j=1;j<=18;j++)
for(int i=1;i<=n;i++)
{
rmqlft[j][i]=rmqlft[j-1][i];
rmqrgt[j][i]=rmqrgt[j-1][i];
int poz=i+(1<<(j-1));
if(poz<=n)
{
rmqlft[j][i]=max(rmqlft[j][i],rmqlft[j-1][poz]);
rmqrgt[j][i]=max(rmqrgt[j][i],rmqrgt[j-1][poz]);
}
}
ll q;
cin>>q;
while(q--)
{
ll x;
cin>>x;
ll ans=0;
ll poz=0;
ll st=1;
ll dr=n;
while(st<=dr)
{
ll mij=(st+dr)/2;
if(v[mij]<=x)
{
poz=mij;
st=mij+1;
}
else
dr=mij-1;
}
if(poz==0)
{
ans=abs(v[1]-x);
poz=1;
}
else if(poz==n)
ans=abs(v[n]-x);
else
{
ll dL=abs(v[poz]-x);
ll dR=abs(v[poz+1]-x);
if(dL<=dR)
ans+=dL;
else
{
ans=ans+dR;
poz++;
}
}
if(n==1)
{
cout<<ans<<'\n';
continue;
}
int l=0,r=0;
int dir=0; /// 0 -> left, 1 -> right
if(poz==1)
{
ans+=abs(v[poz+1]-v[poz]);
r=poz+1;
l=poz-1;
dir=1;
}
else if(poz==n)
{
ans+=abs(v[poz]-v[poz-1]);
l=poz-1;
r=poz+1;
dir=0;
}
else
{
int dL=abs(v[poz]-v[poz-1]);
int dR=abs(v[poz]-v[poz+1]);
l=poz-1;
r=poz+1;
if(dL<=dR)
{
dir=0;
ans+=dL;
}
else
{
dir=1;
ans+=dR;
}
}
while(!finish(l,r))
{
if(l==0)
{
ans+=v[n]-v[r];
break;
}
if(r==n+1)
{
ans+=v[l]-v[1];
break;
}
if(l==1&&r==n)
{
ans+=v[n]-v[1];
break;
}
if(dir==0)
{
int last=l;
int st=2;
int dr=last;
while(st<=dr)
{
int mij=(st+dr)/2;
ll val=getlft(mij,l);
val+=(v[n]-v[r]);
if(val<=0)
{
last=mij-1;
dr=mij-1;
}
else
st=mij+1;
}
ans+=v[l]-v[last];
l=last;
ans+=v[r]-v[l];
l--;
dir^=1;
}
else
{
int last=r;
int st=r;
int dr=n-1;
while(st<=dr)
{
int mij=(st+dr)/2;
ll val=getrgt(r,mij);
val+=(v[l]-v[1]);
if(val<0)
{
last=mij+1;
st=mij+1;
}
else
dr=mij-1;
}
ans+=v[last]-v[r];
r=last;
ans+=v[r]-v[l];
r++;
dir^=1;
}
}
cout<<ans<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
67924 KB |
Output is correct |
2 |
Correct |
10 ms |
68404 KB |
Output is correct |
3 |
Correct |
8 ms |
68188 KB |
Output is correct |
4 |
Correct |
8 ms |
68184 KB |
Output is correct |
5 |
Correct |
8 ms |
68188 KB |
Output is correct |
6 |
Correct |
9 ms |
68124 KB |
Output is correct |
7 |
Correct |
8 ms |
68188 KB |
Output is correct |
8 |
Correct |
9 ms |
68180 KB |
Output is correct |
9 |
Correct |
7 ms |
67932 KB |
Output is correct |
10 |
Correct |
7 ms |
68168 KB |
Output is correct |
11 |
Correct |
9 ms |
68188 KB |
Output is correct |
12 |
Correct |
7 ms |
67932 KB |
Output is correct |
13 |
Correct |
9 ms |
68108 KB |
Output is correct |
14 |
Correct |
8 ms |
68188 KB |
Output is correct |
15 |
Correct |
8 ms |
68188 KB |
Output is correct |
16 |
Correct |
8 ms |
68188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
67924 KB |
Output is correct |
2 |
Correct |
10 ms |
68404 KB |
Output is correct |
3 |
Correct |
8 ms |
68188 KB |
Output is correct |
4 |
Correct |
8 ms |
68184 KB |
Output is correct |
5 |
Correct |
8 ms |
68188 KB |
Output is correct |
6 |
Correct |
9 ms |
68124 KB |
Output is correct |
7 |
Correct |
8 ms |
68188 KB |
Output is correct |
8 |
Correct |
9 ms |
68180 KB |
Output is correct |
9 |
Correct |
7 ms |
67932 KB |
Output is correct |
10 |
Correct |
7 ms |
68168 KB |
Output is correct |
11 |
Correct |
9 ms |
68188 KB |
Output is correct |
12 |
Correct |
7 ms |
67932 KB |
Output is correct |
13 |
Correct |
9 ms |
68108 KB |
Output is correct |
14 |
Correct |
8 ms |
68188 KB |
Output is correct |
15 |
Correct |
8 ms |
68188 KB |
Output is correct |
16 |
Correct |
8 ms |
68188 KB |
Output is correct |
17 |
Correct |
32 ms |
74304 KB |
Output is correct |
18 |
Correct |
35 ms |
74132 KB |
Output is correct |
19 |
Correct |
32 ms |
74360 KB |
Output is correct |
20 |
Correct |
32 ms |
74336 KB |
Output is correct |
21 |
Correct |
33 ms |
74320 KB |
Output is correct |
22 |
Correct |
33 ms |
74300 KB |
Output is correct |
23 |
Correct |
32 ms |
74324 KB |
Output is correct |
24 |
Correct |
33 ms |
74324 KB |
Output is correct |
25 |
Correct |
35 ms |
74588 KB |
Output is correct |
26 |
Correct |
33 ms |
74352 KB |
Output is correct |
27 |
Correct |
33 ms |
74320 KB |
Output is correct |
28 |
Correct |
33 ms |
74552 KB |
Output is correct |
29 |
Correct |
33 ms |
74300 KB |
Output is correct |
30 |
Correct |
32 ms |
74320 KB |
Output is correct |
31 |
Correct |
33 ms |
74272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
67928 KB |
Output is correct |
2 |
Correct |
8 ms |
67932 KB |
Output is correct |
3 |
Correct |
8 ms |
68164 KB |
Output is correct |
4 |
Correct |
8 ms |
67928 KB |
Output is correct |
5 |
Correct |
8 ms |
67932 KB |
Output is correct |
6 |
Correct |
8 ms |
68188 KB |
Output is correct |
7 |
Correct |
99 ms |
76376 KB |
Output is correct |
8 |
Correct |
105 ms |
76396 KB |
Output is correct |
9 |
Correct |
100 ms |
76368 KB |
Output is correct |
10 |
Correct |
100 ms |
76372 KB |
Output is correct |
11 |
Correct |
102 ms |
76368 KB |
Output is correct |
12 |
Correct |
103 ms |
76364 KB |
Output is correct |
13 |
Correct |
40 ms |
72016 KB |
Output is correct |
14 |
Correct |
43 ms |
69196 KB |
Output is correct |
15 |
Correct |
112 ms |
77764 KB |
Output is correct |
16 |
Correct |
110 ms |
77248 KB |
Output is correct |
17 |
Correct |
111 ms |
77648 KB |
Output is correct |
18 |
Correct |
41 ms |
71760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
67924 KB |
Output is correct |
2 |
Correct |
10 ms |
68404 KB |
Output is correct |
3 |
Correct |
8 ms |
68188 KB |
Output is correct |
4 |
Correct |
8 ms |
68184 KB |
Output is correct |
5 |
Correct |
8 ms |
68188 KB |
Output is correct |
6 |
Correct |
9 ms |
68124 KB |
Output is correct |
7 |
Correct |
8 ms |
68188 KB |
Output is correct |
8 |
Correct |
9 ms |
68180 KB |
Output is correct |
9 |
Correct |
7 ms |
67932 KB |
Output is correct |
10 |
Correct |
7 ms |
68168 KB |
Output is correct |
11 |
Correct |
9 ms |
68188 KB |
Output is correct |
12 |
Correct |
7 ms |
67932 KB |
Output is correct |
13 |
Correct |
9 ms |
68108 KB |
Output is correct |
14 |
Correct |
8 ms |
68188 KB |
Output is correct |
15 |
Correct |
8 ms |
68188 KB |
Output is correct |
16 |
Correct |
8 ms |
68188 KB |
Output is correct |
17 |
Correct |
32 ms |
74304 KB |
Output is correct |
18 |
Correct |
35 ms |
74132 KB |
Output is correct |
19 |
Correct |
32 ms |
74360 KB |
Output is correct |
20 |
Correct |
32 ms |
74336 KB |
Output is correct |
21 |
Correct |
33 ms |
74320 KB |
Output is correct |
22 |
Correct |
33 ms |
74300 KB |
Output is correct |
23 |
Correct |
32 ms |
74324 KB |
Output is correct |
24 |
Correct |
33 ms |
74324 KB |
Output is correct |
25 |
Correct |
35 ms |
74588 KB |
Output is correct |
26 |
Correct |
33 ms |
74352 KB |
Output is correct |
27 |
Correct |
33 ms |
74320 KB |
Output is correct |
28 |
Correct |
33 ms |
74552 KB |
Output is correct |
29 |
Correct |
33 ms |
74300 KB |
Output is correct |
30 |
Correct |
32 ms |
74320 KB |
Output is correct |
31 |
Correct |
33 ms |
74272 KB |
Output is correct |
32 |
Correct |
8 ms |
67928 KB |
Output is correct |
33 |
Correct |
8 ms |
67932 KB |
Output is correct |
34 |
Correct |
8 ms |
68164 KB |
Output is correct |
35 |
Correct |
8 ms |
67928 KB |
Output is correct |
36 |
Correct |
8 ms |
67932 KB |
Output is correct |
37 |
Correct |
8 ms |
68188 KB |
Output is correct |
38 |
Correct |
99 ms |
76376 KB |
Output is correct |
39 |
Correct |
105 ms |
76396 KB |
Output is correct |
40 |
Correct |
100 ms |
76368 KB |
Output is correct |
41 |
Correct |
100 ms |
76372 KB |
Output is correct |
42 |
Correct |
102 ms |
76368 KB |
Output is correct |
43 |
Correct |
103 ms |
76364 KB |
Output is correct |
44 |
Correct |
40 ms |
72016 KB |
Output is correct |
45 |
Correct |
43 ms |
69196 KB |
Output is correct |
46 |
Correct |
112 ms |
77764 KB |
Output is correct |
47 |
Correct |
110 ms |
77248 KB |
Output is correct |
48 |
Correct |
111 ms |
77648 KB |
Output is correct |
49 |
Correct |
41 ms |
71760 KB |
Output is correct |
50 |
Correct |
304 ms |
78416 KB |
Output is correct |
51 |
Correct |
263 ms |
78420 KB |
Output is correct |
52 |
Correct |
235 ms |
78364 KB |
Output is correct |
53 |
Correct |
171 ms |
78316 KB |
Output is correct |
54 |
Correct |
180 ms |
78320 KB |
Output is correct |
55 |
Correct |
169 ms |
78344 KB |
Output is correct |
56 |
Correct |
87 ms |
78164 KB |
Output is correct |
57 |
Correct |
87 ms |
78164 KB |
Output is correct |
58 |
Correct |
86 ms |
78264 KB |
Output is correct |