#include<bits/stdc++.h>
using namespace std;
/*ifstream fin("input.in");
ofstream fout("output.out");
#define cin fin
#define cout fout*/
#define int long long
int n;
int a[200005];
int rmq[2][18][200005];
int p2[200005];
int qry(int le, int ri, int c)
{
int p = p2[ri-le+1];
return max(rmq[c][p][le], rmq[c][p][ri-(1<<p)+1]);
}
int solve(int le, int ri, int poz)
{
if(le==1 && ri==n)
return 0;
if(le==1)
{
return a[n]-poz;
}
else if(ri==n)
{
return poz-a[1];
}
if(le>1 && (ri==n || poz-a[le-1] <= a[ri+1]-poz))///merge spre stanga
{
int st=2,dr=le,ans=le;
while(st<=dr)
{
int mij=(st+dr)/2;
if(qry(mij,le,0) <= a[ri+1])
{
ans=mij;
dr=mij-1;
}
else
st=mij+1;
}
return solve(ans-1,ri,a[ans-1]) + poz-a[ans-1];
}
else///merge spre dreapta
{
int st=ri+1,dr=n,ans=ri+1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(qry(ri+1,mij,1) < -a[le-1])
{
ans=mij;
st=mij+1;
}
else
dr=mij-1;
}
return solve(le,ans,a[ans]) + a[ans]-poz;
}
}
///a[x]-a[x-1] <= a[ri]-a[x]
///2*a[x]-a[x-1] <= a[ri]
///a[x]-a[x-1] <= a[x-1] - a[le]
///a[x]-2*a[x-1] <= -a[le]
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
rmq[0][0][i] = 2*a[i]-a[i-1];
rmq[1][0][i] = a[i]-2*a[i-1];
}
for(int p=1;p<18;p++)
{
for(int i=1;i+(1<<p)-1<=n;i++)
{
rmq[0][p][i] = max(rmq[0][p-1][i], rmq[0][p-1][i+(1<<(p-1))]);
rmq[1][p][i] = max(rmq[1][p-1][i], rmq[1][p-1][i+(1<<(p-1))]);
}
}
for(int i=1;i<=n;i++)
{
p2[i]=0;
while(p2[i]+1<18 && (1<<(p2[i]+1))<=i)
p2[i]++;
}
int q,s;
cin>>q;
while(q--)
{
cin>>s;
if(s<=a[1])
cout<<solve(1,1,a[1])+(a[1]-s)<<"\n";
else if(s>=a[n])
cout<<solve(n,n,a[n])+(s-a[n])<<"\n";
else
{
int st=1,dr=n-1,ans=n;
while(st<=dr)
{
int mij=(st+dr)/2;
if(a[mij]>=s)
{
ans=mij;
dr=mij-1;
}
else
st=mij+1;
}
if(ans==1 || a[ans]-s < s-a[ans-1]) cout<<solve(ans,ans,a[ans])+(a[ans]-s)<<"\n";
else cout<<solve(ans-1,ans-1,a[ans-1])+(s-a[ans-1])<<"\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
796 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
796 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
988 KB |
Output is correct |
17 |
Correct |
40 ms |
57856 KB |
Output is correct |
18 |
Correct |
40 ms |
57684 KB |
Output is correct |
19 |
Correct |
41 ms |
57680 KB |
Output is correct |
20 |
Correct |
40 ms |
57684 KB |
Output is correct |
21 |
Correct |
42 ms |
57764 KB |
Output is correct |
22 |
Correct |
43 ms |
57680 KB |
Output is correct |
23 |
Correct |
42 ms |
57680 KB |
Output is correct |
24 |
Correct |
51 ms |
57684 KB |
Output is correct |
25 |
Correct |
40 ms |
57680 KB |
Output is correct |
26 |
Correct |
41 ms |
57684 KB |
Output is correct |
27 |
Correct |
46 ms |
57680 KB |
Output is correct |
28 |
Correct |
43 ms |
57684 KB |
Output is correct |
29 |
Correct |
40 ms |
57684 KB |
Output is correct |
30 |
Correct |
40 ms |
57676 KB |
Output is correct |
31 |
Correct |
51 ms |
57764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
102 ms |
59852 KB |
Output is correct |
8 |
Correct |
92 ms |
59848 KB |
Output is correct |
9 |
Correct |
107 ms |
59732 KB |
Output is correct |
10 |
Correct |
94 ms |
59732 KB |
Output is correct |
11 |
Correct |
98 ms |
59728 KB |
Output is correct |
12 |
Correct |
101 ms |
59836 KB |
Output is correct |
13 |
Correct |
27 ms |
4180 KB |
Output is correct |
14 |
Correct |
18 ms |
1628 KB |
Output is correct |
15 |
Correct |
102 ms |
60904 KB |
Output is correct |
16 |
Correct |
95 ms |
60916 KB |
Output is correct |
17 |
Correct |
104 ms |
60808 KB |
Output is correct |
18 |
Correct |
27 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
796 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
988 KB |
Output is correct |
17 |
Correct |
40 ms |
57856 KB |
Output is correct |
18 |
Correct |
40 ms |
57684 KB |
Output is correct |
19 |
Correct |
41 ms |
57680 KB |
Output is correct |
20 |
Correct |
40 ms |
57684 KB |
Output is correct |
21 |
Correct |
42 ms |
57764 KB |
Output is correct |
22 |
Correct |
43 ms |
57680 KB |
Output is correct |
23 |
Correct |
42 ms |
57680 KB |
Output is correct |
24 |
Correct |
51 ms |
57684 KB |
Output is correct |
25 |
Correct |
40 ms |
57680 KB |
Output is correct |
26 |
Correct |
41 ms |
57684 KB |
Output is correct |
27 |
Correct |
46 ms |
57680 KB |
Output is correct |
28 |
Correct |
43 ms |
57684 KB |
Output is correct |
29 |
Correct |
40 ms |
57684 KB |
Output is correct |
30 |
Correct |
40 ms |
57676 KB |
Output is correct |
31 |
Correct |
51 ms |
57764 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
102 ms |
59852 KB |
Output is correct |
39 |
Correct |
92 ms |
59848 KB |
Output is correct |
40 |
Correct |
107 ms |
59732 KB |
Output is correct |
41 |
Correct |
94 ms |
59732 KB |
Output is correct |
42 |
Correct |
98 ms |
59728 KB |
Output is correct |
43 |
Correct |
101 ms |
59836 KB |
Output is correct |
44 |
Correct |
27 ms |
4180 KB |
Output is correct |
45 |
Correct |
18 ms |
1628 KB |
Output is correct |
46 |
Correct |
102 ms |
60904 KB |
Output is correct |
47 |
Correct |
95 ms |
60916 KB |
Output is correct |
48 |
Correct |
104 ms |
60808 KB |
Output is correct |
49 |
Correct |
27 ms |
4176 KB |
Output is correct |
50 |
Correct |
224 ms |
61848 KB |
Output is correct |
51 |
Correct |
217 ms |
61968 KB |
Output is correct |
52 |
Correct |
248 ms |
61860 KB |
Output is correct |
53 |
Correct |
163 ms |
61780 KB |
Output is correct |
54 |
Correct |
153 ms |
61776 KB |
Output is correct |
55 |
Correct |
148 ms |
61776 KB |
Output is correct |
56 |
Correct |
85 ms |
61780 KB |
Output is correct |
57 |
Correct |
85 ms |
61640 KB |
Output is correct |
58 |
Correct |
86 ms |
61864 KB |
Output is correct |