#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll lg[200005];
struct Sparse{
vector<vector<ll> >stmin,stmax;
vector<ll>arr;
void init(ll n){
stmin.resize(n+5),stmax.resize(n+5),arr.resize(n+5);
for(int i=0;i<n+5;i++){
stmax[i].resize(21);
stmin[i].resize(21);
}
}
void build(ll n){
for(int i=1;i<=n;i++){
stmax[i][0]=stmin[i][0]=arr[i];
}
for(int j=1;j<=20;j++){
for(int i=1;i<=n;i++){
if(i+(1<<(j-1))<=n){
stmax[i][j]=max(stmax[i][j-1],stmax[i+(1<<(j-1))][j-1]);
stmin[i][j]=min(stmin[i][j-1],stmin[i+(1<<(j-1))][j-1]);
}
}
}
}
ll maximum(ll ql, ll qr){
ll rng=lg[qr-ql+1];
return max(stmax[ql][rng],stmax[qr-(1<<rng)+1][rng]);
}
ll minimum(ll ql, ll qr){
ll rng=lg[qr-ql+1];
return min(stmin[ql][rng],stmin[qr-(1<<rng)+1][rng]);
}
}to_left,to_right;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for(int i=2;i<=200000;i*=2){
lg[i]=1;
}
for(int i=3;i<=200000;i++){
lg[i]+=lg[i-1];
}
ll n;
cin>>n;
to_left.init(n),to_right.init(n);
ll x[n+5];map<ll,ll>mp;
x[0]=x[n+1]=1e18;
vector<ll>rev,xx;
rev.push_back(1e18);
for(int i=1;i<=n;i++){
cin>>x[i];mp[x[i]]=i;
rev.push_back(-x[i]);
xx.push_back(x[i]);
}
for(int i=1;i<=n-1;i++){
to_left.arr[i]=2*x[i+1]-x[i];
}
for(int i=2;i<=n;i++){
to_right.arr[i]=2*x[i-1]-x[i];
}
to_left.build(n),to_right.build(n);
// x[k]>=min(2*x[i+1]-x[i])
// x[k]<2*x[i-1]-x[i]
reverse(rev.begin(),rev.end());
xx.push_back(1e18);
ll q;
cin>>q;
while(q--){
ll k;
cin>>k;
ll bpos=lower_bound(xx.begin(),xx.end(),k)-xx.begin()+1;
ll fpos=n+1-(lower_bound(rev.begin(),rev.end(),-k)-rev.begin()+1);
ll starting=0;
if(abs(x[fpos]-k)<=abs(x[bpos]-k)){
starting=fpos;
}
else{
starting=bpos;
}
ll l=starting,r=starting,pos=x[starting],ans=abs(k-x[starting]);
while(l!=1&&r!=n){
ll lb=1,rb=l-1;
if(pos-x[l-1]<=x[r+1]-pos){
while(lb<rb){
ll mid=(lb+rb)>>1;
if(to_left.maximum(mid,l-1)<=x[r+1]){
rb=mid;
}
else lb=mid+1;
}
ans+=(pos-x[lb]);pos=x[lb];l=lb;
// cout<<lb<<' ';
}
if(l==1||r==n)break;
if(x[r+1]-pos<pos-x[l-1]){
lb=r+1,rb=n;
while(lb<rb){
ll mid=(lb+rb+1)>>1;
if(to_right.minimum(r+1,mid)>x[l-1]){
lb=mid;
}
else rb=mid-1;
}
ans+=(x[rb]-pos);pos=x[rb];r=rb;
// cout<<lb<<" ";
}
if(l==1||r==n)break;
}
if(l==1){
ans+=x[n]-pos;
}
else{
ans+=pos-x[1];
}
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
2 ms |
3676 KB |
Output is correct |
5 |
Correct |
2 ms |
3676 KB |
Output is correct |
6 |
Correct |
3 ms |
3676 KB |
Output is correct |
7 |
Correct |
3 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 KB |
Output is correct |
15 |
Correct |
2 ms |
3816 KB |
Output is correct |
16 |
Correct |
3 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
2 ms |
3676 KB |
Output is correct |
5 |
Correct |
2 ms |
3676 KB |
Output is correct |
6 |
Correct |
3 ms |
3676 KB |
Output is correct |
7 |
Correct |
3 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 KB |
Output is correct |
15 |
Correct |
2 ms |
3816 KB |
Output is correct |
16 |
Correct |
3 ms |
3796 KB |
Output is correct |
17 |
Correct |
231 ms |
180788 KB |
Output is correct |
18 |
Correct |
253 ms |
180792 KB |
Output is correct |
19 |
Correct |
245 ms |
180784 KB |
Output is correct |
20 |
Correct |
251 ms |
180864 KB |
Output is correct |
21 |
Correct |
243 ms |
181132 KB |
Output is correct |
22 |
Correct |
235 ms |
180784 KB |
Output is correct |
23 |
Correct |
246 ms |
180724 KB |
Output is correct |
24 |
Correct |
259 ms |
180724 KB |
Output is correct |
25 |
Correct |
242 ms |
180784 KB |
Output is correct |
26 |
Correct |
234 ms |
180784 KB |
Output is correct |
27 |
Correct |
231 ms |
180828 KB |
Output is correct |
28 |
Correct |
251 ms |
181048 KB |
Output is correct |
29 |
Correct |
232 ms |
180628 KB |
Output is correct |
30 |
Correct |
230 ms |
180788 KB |
Output is correct |
31 |
Correct |
248 ms |
180788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
343 ms |
180912 KB |
Output is correct |
8 |
Correct |
379 ms |
182680 KB |
Output is correct |
9 |
Correct |
367 ms |
182832 KB |
Output is correct |
10 |
Correct |
355 ms |
182624 KB |
Output is correct |
11 |
Correct |
351 ms |
182840 KB |
Output is correct |
12 |
Correct |
345 ms |
182880 KB |
Output is correct |
13 |
Correct |
31 ms |
5712 KB |
Output is correct |
14 |
Correct |
23 ms |
3176 KB |
Output is correct |
15 |
Correct |
354 ms |
183860 KB |
Output is correct |
16 |
Correct |
335 ms |
183856 KB |
Output is correct |
17 |
Correct |
348 ms |
183860 KB |
Output is correct |
18 |
Correct |
31 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
2 ms |
3676 KB |
Output is correct |
5 |
Correct |
2 ms |
3676 KB |
Output is correct |
6 |
Correct |
3 ms |
3676 KB |
Output is correct |
7 |
Correct |
3 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 KB |
Output is correct |
15 |
Correct |
2 ms |
3816 KB |
Output is correct |
16 |
Correct |
3 ms |
3796 KB |
Output is correct |
17 |
Correct |
231 ms |
180788 KB |
Output is correct |
18 |
Correct |
253 ms |
180792 KB |
Output is correct |
19 |
Correct |
245 ms |
180784 KB |
Output is correct |
20 |
Correct |
251 ms |
180864 KB |
Output is correct |
21 |
Correct |
243 ms |
181132 KB |
Output is correct |
22 |
Correct |
235 ms |
180784 KB |
Output is correct |
23 |
Correct |
246 ms |
180724 KB |
Output is correct |
24 |
Correct |
259 ms |
180724 KB |
Output is correct |
25 |
Correct |
242 ms |
180784 KB |
Output is correct |
26 |
Correct |
234 ms |
180784 KB |
Output is correct |
27 |
Correct |
231 ms |
180828 KB |
Output is correct |
28 |
Correct |
251 ms |
181048 KB |
Output is correct |
29 |
Correct |
232 ms |
180628 KB |
Output is correct |
30 |
Correct |
230 ms |
180788 KB |
Output is correct |
31 |
Correct |
248 ms |
180788 KB |
Output is correct |
32 |
Correct |
1 ms |
1880 KB |
Output is correct |
33 |
Correct |
1 ms |
1884 KB |
Output is correct |
34 |
Correct |
1 ms |
1884 KB |
Output is correct |
35 |
Correct |
1 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
1884 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
343 ms |
180912 KB |
Output is correct |
39 |
Correct |
379 ms |
182680 KB |
Output is correct |
40 |
Correct |
367 ms |
182832 KB |
Output is correct |
41 |
Correct |
355 ms |
182624 KB |
Output is correct |
42 |
Correct |
351 ms |
182840 KB |
Output is correct |
43 |
Correct |
345 ms |
182880 KB |
Output is correct |
44 |
Correct |
31 ms |
5712 KB |
Output is correct |
45 |
Correct |
23 ms |
3176 KB |
Output is correct |
46 |
Correct |
354 ms |
183860 KB |
Output is correct |
47 |
Correct |
335 ms |
183856 KB |
Output is correct |
48 |
Correct |
348 ms |
183860 KB |
Output is correct |
49 |
Correct |
31 ms |
5720 KB |
Output is correct |
50 |
Correct |
576 ms |
184888 KB |
Output is correct |
51 |
Correct |
520 ms |
184888 KB |
Output is correct |
52 |
Correct |
519 ms |
185004 KB |
Output is correct |
53 |
Correct |
434 ms |
184840 KB |
Output is correct |
54 |
Correct |
432 ms |
184916 KB |
Output is correct |
55 |
Correct |
457 ms |
184880 KB |
Output is correct |
56 |
Correct |
296 ms |
184628 KB |
Output is correct |
57 |
Correct |
302 ms |
184880 KB |
Output is correct |
58 |
Correct |
291 ms |
184628 KB |
Output is correct |