#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
// lst: max
// rst: min
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector<int> vec(n+1,-1e9);
for(int i=1;i<=n;i++) cin>>vec[i];
vector<vector<int> > m(2,vector<int>(n+1)),lst(20,vector<int>(n+1)),rst(20,vector<int>(n+1)); // [0]=left [1]=right
for(int i=1;i<n;i++) m[0][i]=2*vec[i+1]-vec[i],lst[0][i]=m[0][i];
for(int i=2;i<=n;i++) m[1][i]=2*vec[i-1]-vec[i],rst[0][i]=m[1][i];
for(int j=1;j<20;j++){
for(int i=1;i+(1<<(j-1))<n;i++) lst[j][i]=max(lst[j-1][i],lst[j-1][i+(1<<(j-1))]);
for(int i=2;i+(1<<(j-1))<=n;i++) rst[j][i]=min(rst[j-1][i],rst[j-1][i+(1<<(j-1))]);
}
vector<int> bases(n+1);
for(int j=0;j<20;j++){
for(int i=(1<<j);i<=min(1<<(j+1),n);i++) bases[i]=j;
}
auto query=[&](int l,int r,bool flag){
int len=bases[r-l+1];
if(flag) return min(rst[len][l],rst[len][r-(1<<len)+1]);
else return max(lst[len][l],lst[len][r-(1<<len)+1]);
};
vector<ll> ans(n+1);
//cout<<query(3,4,1)<<" "<<query(3,5,1)<<"\n";
for(int i=1;i<=n;i++){
if(i==1||i==n){
ans[i]=vec[n]-vec[1];
continue;
}
bool dire;
int curl=i-1,curr=i+1;
if(vec[i]-vec[curl]<=vec[curr]-vec[i]) dire=0,ans[i]+=vec[i]-vec[curl];
else dire=1,ans[i]+=vec[curr]-vec[i];
//cout<<dire;
while(1){
//cout<<i<<" "<<curl<<" "<<curr<<"\n";
if(!dire){
int l=0,r=curl;
while(l<r){
int mid=((l+r)>>1)+1;
if(query(mid,curl,0)>vec[curr]){
l=mid;
}
else r=mid-1;
}
if(l==0){
ans[i]+=1ll*(vec[curl]-vec[1]+vec[n]-vec[1]);
break;
}
else{
ans[i]+=1ll*(vec[curl]-vec[l+1]+vec[curr]-vec[l+1]);
curl=l;
dire=1;
}
}
else{
int l=curr,r=n+1;
while(l<r){
int mid=((l+r)>>1);
if(query(curr,mid,1)<=vec[curl]){
r=mid;
}
else l=mid+1;
}
//cout<<l;
if(l==n+1){
ans[i]+=1ll*(vec[n]-vec[curr]+vec[n]-vec[1]);
break;
}
else{
ans[i]+=1ll*(vec[r-1]-vec[curr]+vec[r-1]-vec[curl]);
curr=r;
dire=0;
}
}
}
}
int q;
cin>>q;
ll num;
for(int i=0;i<q;i++){
cin>>num;
if(num<vec[1]) cout<<vec[n]-num<<"\n";
else if(num>vec[n]) cout<<num-vec[1]<<"\n";
else{
int pos=lower_bound(vec.begin(),vec.end(),num)-vec.begin();
if(vec[pos]==num) cout<<ans[pos]<<"\n";
else{
if(num-vec[pos-1]<=vec[pos]-num) cout<<ans[pos-1]+num-vec[pos-1]<<"\n";
else cout<<vec[pos]-num+ans[pos]<<"\n";
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
792 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
792 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
740 KB |
Output is correct |
17 |
Correct |
110 ms |
38408 KB |
Output is correct |
18 |
Correct |
110 ms |
38204 KB |
Output is correct |
19 |
Correct |
118 ms |
38400 KB |
Output is correct |
20 |
Correct |
115 ms |
38212 KB |
Output is correct |
21 |
Correct |
112 ms |
38192 KB |
Output is correct |
22 |
Correct |
113 ms |
38404 KB |
Output is correct |
23 |
Correct |
116 ms |
38208 KB |
Output is correct |
24 |
Correct |
76 ms |
38212 KB |
Output is correct |
25 |
Correct |
74 ms |
38208 KB |
Output is correct |
26 |
Correct |
73 ms |
38212 KB |
Output is correct |
27 |
Correct |
77 ms |
38412 KB |
Output is correct |
28 |
Correct |
77 ms |
38400 KB |
Output is correct |
29 |
Correct |
183 ms |
38212 KB |
Output is correct |
30 |
Correct |
214 ms |
38212 KB |
Output is correct |
31 |
Correct |
187 ms |
38324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
108 ms |
40416 KB |
Output is correct |
8 |
Correct |
120 ms |
40352 KB |
Output is correct |
9 |
Correct |
103 ms |
40256 KB |
Output is correct |
10 |
Correct |
110 ms |
40188 KB |
Output is correct |
11 |
Correct |
106 ms |
40164 KB |
Output is correct |
12 |
Correct |
103 ms |
40256 KB |
Output is correct |
13 |
Correct |
30 ms |
4188 KB |
Output is correct |
14 |
Correct |
21 ms |
1512 KB |
Output is correct |
15 |
Correct |
110 ms |
41320 KB |
Output is correct |
16 |
Correct |
109 ms |
41284 KB |
Output is correct |
17 |
Correct |
117 ms |
41540 KB |
Output is correct |
18 |
Correct |
38 ms |
4100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
792 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
740 KB |
Output is correct |
17 |
Correct |
110 ms |
38408 KB |
Output is correct |
18 |
Correct |
110 ms |
38204 KB |
Output is correct |
19 |
Correct |
118 ms |
38400 KB |
Output is correct |
20 |
Correct |
115 ms |
38212 KB |
Output is correct |
21 |
Correct |
112 ms |
38192 KB |
Output is correct |
22 |
Correct |
113 ms |
38404 KB |
Output is correct |
23 |
Correct |
116 ms |
38208 KB |
Output is correct |
24 |
Correct |
76 ms |
38212 KB |
Output is correct |
25 |
Correct |
74 ms |
38208 KB |
Output is correct |
26 |
Correct |
73 ms |
38212 KB |
Output is correct |
27 |
Correct |
77 ms |
38412 KB |
Output is correct |
28 |
Correct |
77 ms |
38400 KB |
Output is correct |
29 |
Correct |
183 ms |
38212 KB |
Output is correct |
30 |
Correct |
214 ms |
38212 KB |
Output is correct |
31 |
Correct |
187 ms |
38324 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 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 |
108 ms |
40416 KB |
Output is correct |
39 |
Correct |
120 ms |
40352 KB |
Output is correct |
40 |
Correct |
103 ms |
40256 KB |
Output is correct |
41 |
Correct |
110 ms |
40188 KB |
Output is correct |
42 |
Correct |
106 ms |
40164 KB |
Output is correct |
43 |
Correct |
103 ms |
40256 KB |
Output is correct |
44 |
Correct |
30 ms |
4188 KB |
Output is correct |
45 |
Correct |
21 ms |
1512 KB |
Output is correct |
46 |
Correct |
110 ms |
41320 KB |
Output is correct |
47 |
Correct |
109 ms |
41284 KB |
Output is correct |
48 |
Correct |
117 ms |
41540 KB |
Output is correct |
49 |
Correct |
38 ms |
4100 KB |
Output is correct |
50 |
Correct |
163 ms |
42308 KB |
Output is correct |
51 |
Correct |
167 ms |
42308 KB |
Output is correct |
52 |
Correct |
168 ms |
42308 KB |
Output is correct |
53 |
Correct |
141 ms |
42468 KB |
Output is correct |
54 |
Correct |
141 ms |
42424 KB |
Output is correct |
55 |
Correct |
133 ms |
42464 KB |
Output is correct |
56 |
Correct |
231 ms |
42352 KB |
Output is correct |
57 |
Correct |
222 ms |
42588 KB |
Output is correct |
58 |
Correct |
220 ms |
42224 KB |
Output is correct |