#include<bits/stdc++.h>
using namespace std;
//#include<bits/extc++.h>
//__gnu_pbds
vector<long long> x;
vector<int> rlim[20];
vector<int> llim[20];
int n;
long long ans = 0;
int getmax(int l,int r){
int len = (r-l+1);
len = __lg(len);
return max(rlim[len][l],rlim[len][r-(1<<(len))+1]);
}
int getmin(int l,int r){
int len = (r-l+1);
len = __lg(len);
return min(llim[len][l],llim[len][r-(1<<len)+1]);
}
void solve(){
int a;cin>>a;
// cout<<a<<endl;
int k = lower_bound(x.begin(),x.end(),a)-x.begin();
if(abs(x[k-1]-a)<=abs(x[k]-a)) k--;
ans=abs(x[k]-a);
int cur = k;
int l = k;int r = k;
while(l!=1 || r!=n){
//cout<<ans<<"\n";
//cout<<l<<" "<<r<<"\n";
if(abs(x[l-1]-x[cur])<=abs(x[r+1]-x[cur])){
ans+=abs(x[l-1]-x[cur]);
cur = l-1;
int rr = l;
int ll = 1;
while(rr-ll>1){
int mid = (rr+ll)/2;
if(getmax(mid,l-1)>=r) ll = mid;
else rr = mid;
}
ans+=abs(x[ll]-x[cur]);
l = ll;
cur = l;
}else{
ans+=abs(x[r+1]-x[cur]);
cur = r+1;
int rr = n;
int ll = r;
while(rr-ll>1){
int mid = (rr+ll)/2;
if(getmin(r+1,mid)<=l) rr = mid;
else ll = mid;
}
ans+=abs(x[rr]-x[cur]);
r = rr;
cur = r;
}
}
cout<<ans<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
x.resize(n+2);
for(int p=0;p<20;p++){
rlim[p].resize(n+2);
llim[p].resize(n+2);
}
x[0]=-1e15;
x[n+1]=1e15;
for(int i=1;i<=n;i++) cin>>x[i];
for(int i=1;i<=n;i++){
int l = i;int r = n+1;
while(r-l>1){
int mid = (l+r)/2;
if(abs(x[i]-x[i-1])>abs(x[mid]-x[i])) l = mid;
else r = mid;
}
rlim[0][i]=l;
}
for(int i=1;i<=n;i++){
int r = i;int l = 0;
while(r-l>1){
int mid = (l+r)/2;
if(abs(x[i]-x[i+1])>=abs(x[mid]-x[i])) r = mid;
else l = mid;
}
llim[0][i]=r;
}
for(int p=1;p<20;p++){
for(int i=1;i+(1<<(p-1))<=n;i++){
rlim[p][i] = max(rlim[p-1][i],rlim[p-1][i+(1<<(p-1))]);
llim[p][i] = min(llim[p-1][i],llim[p-1][i+(1<<(p-1))]);
}
}
int q;cin>>q;
while(q--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
804 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
728 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
804 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
728 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
65 ms |
35376 KB |
Output is correct |
18 |
Correct |
65 ms |
35208 KB |
Output is correct |
19 |
Correct |
61 ms |
35152 KB |
Output is correct |
20 |
Correct |
61 ms |
35368 KB |
Output is correct |
21 |
Correct |
62 ms |
35164 KB |
Output is correct |
22 |
Correct |
61 ms |
35156 KB |
Output is correct |
23 |
Correct |
61 ms |
35164 KB |
Output is correct |
24 |
Correct |
61 ms |
35156 KB |
Output is correct |
25 |
Correct |
62 ms |
35164 KB |
Output is correct |
26 |
Correct |
61 ms |
35364 KB |
Output is correct |
27 |
Correct |
67 ms |
35192 KB |
Output is correct |
28 |
Correct |
60 ms |
35164 KB |
Output is correct |
29 |
Correct |
61 ms |
35316 KB |
Output is correct |
30 |
Correct |
61 ms |
35152 KB |
Output is correct |
31 |
Correct |
60 ms |
35156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
202 ms |
37144 KB |
Output is correct |
8 |
Correct |
202 ms |
37240 KB |
Output is correct |
9 |
Correct |
206 ms |
37356 KB |
Output is correct |
10 |
Correct |
197 ms |
37332 KB |
Output is correct |
11 |
Correct |
196 ms |
37132 KB |
Output is correct |
12 |
Correct |
200 ms |
37372 KB |
Output is correct |
13 |
Correct |
32 ms |
4176 KB |
Output is correct |
14 |
Correct |
23 ms |
1488 KB |
Output is correct |
15 |
Correct |
367 ms |
38472 KB |
Output is correct |
16 |
Correct |
338 ms |
38228 KB |
Output is correct |
17 |
Correct |
345 ms |
38228 KB |
Output is correct |
18 |
Correct |
38 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
804 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
728 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
65 ms |
35376 KB |
Output is correct |
18 |
Correct |
65 ms |
35208 KB |
Output is correct |
19 |
Correct |
61 ms |
35152 KB |
Output is correct |
20 |
Correct |
61 ms |
35368 KB |
Output is correct |
21 |
Correct |
62 ms |
35164 KB |
Output is correct |
22 |
Correct |
61 ms |
35156 KB |
Output is correct |
23 |
Correct |
61 ms |
35164 KB |
Output is correct |
24 |
Correct |
61 ms |
35156 KB |
Output is correct |
25 |
Correct |
62 ms |
35164 KB |
Output is correct |
26 |
Correct |
61 ms |
35364 KB |
Output is correct |
27 |
Correct |
67 ms |
35192 KB |
Output is correct |
28 |
Correct |
60 ms |
35164 KB |
Output is correct |
29 |
Correct |
61 ms |
35316 KB |
Output is correct |
30 |
Correct |
61 ms |
35152 KB |
Output is correct |
31 |
Correct |
60 ms |
35156 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
456 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
202 ms |
37144 KB |
Output is correct |
39 |
Correct |
202 ms |
37240 KB |
Output is correct |
40 |
Correct |
206 ms |
37356 KB |
Output is correct |
41 |
Correct |
197 ms |
37332 KB |
Output is correct |
42 |
Correct |
196 ms |
37132 KB |
Output is correct |
43 |
Correct |
200 ms |
37372 KB |
Output is correct |
44 |
Correct |
32 ms |
4176 KB |
Output is correct |
45 |
Correct |
23 ms |
1488 KB |
Output is correct |
46 |
Correct |
367 ms |
38472 KB |
Output is correct |
47 |
Correct |
338 ms |
38228 KB |
Output is correct |
48 |
Correct |
345 ms |
38228 KB |
Output is correct |
49 |
Correct |
38 ms |
4432 KB |
Output is correct |
50 |
Correct |
809 ms |
39616 KB |
Output is correct |
51 |
Correct |
665 ms |
39740 KB |
Output is correct |
52 |
Correct |
661 ms |
39508 KB |
Output is correct |
53 |
Correct |
601 ms |
39512 KB |
Output is correct |
54 |
Correct |
600 ms |
39348 KB |
Output is correct |
55 |
Correct |
592 ms |
39548 KB |
Output is correct |
56 |
Correct |
653 ms |
39384 KB |
Output is correct |
57 |
Correct |
720 ms |
39368 KB |
Output is correct |
58 |
Correct |
724 ms |
39384 KB |
Output is correct |