// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int inf=2e9;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=200005;
const int bl=650;
const int maxs=650;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
int n,q,x[maxn],down[maxn],up[maxn],Maxup[maxn][maxl],Mindown[maxn][maxl],lg2[maxn];
int check_up(int l,int r){
int p=lg2[r-l+1];
return max(Maxup[l][p],Maxup[r-(1<<p)+1][p]);
}
int check_down(int l,int r){
int p=lg2[r-l+1];
return min(Mindown[l][p],Mindown[r-(1<<p)+1][p]);
}
int cal(int a){
int r=lower_bound(x+1,x+n+1,a)-x,l=r-1;
long long ans=0;
while(true){
//cout << a << ' ' << l << ' ' << r << '\n';
if(l==0){ans+=x[n]-a;break;}
else if(r==n+1){ans+=a-x[1];break;}
if(x[r]-a<a-x[l]){
int lt=r,rt=n-1,pos=r;
while(rt>=lt){
int mid=(lt+rt)>>1;
if(check_down(r,mid)>l){pos=mid+1;lt=mid+1;}
else rt=mid-1;
}
ans+=x[pos]-a;
a=x[pos];r=pos+1;
}
else{
int lt=2,rt=l,pos=l;
while(rt>=lt){
int mid=(lt+rt)>>1;
//cout << '*' << mid << ' ' << l << ' ' << check_up(mid,l) << '\n';
if(check_up(mid,l)<r){pos=mid-1;rt=mid-1;}
else lt=mid+1;
}
//cout << pos << '\n';
ans+=a-x[pos];
a=x[pos];l=pos-1;
}
}
return ans;
}
void buildupdown(){
for(int i=1;i<n;i++){
int l=1,r=i;
while(r>=l){
int mid=(r+l)>>1;
if(x[i]-x[mid]<=x[i+1]-x[i]){down[i]=mid;r=mid-1;}
else l=mid+1;
}
Mindown[i][0]=down[i];
}
for(int i=2;i<=n;i++){
int l=i,r=n;
while(r>=l){
int mid=(l+r)>>1;
if(x[mid]-x[i]<x[i]-x[i-1]){up[i]=mid;l=mid+1;}
else r=mid-1;
}
Maxup[i][0]=up[i];
}
for(int i=2;i<=n;i++) lg2[i]=lg2[i/2]+1;
for(int i=1;i<=18;i++){
for(int j=1;j<=(n-(1<<i)+1);j++){
Maxup[j][i]=max(Maxup[j][i-1],Maxup[j+(1<<(i-1))][i-1]);
Mindown[j][i]=min(Mindown[j][i-1],Mindown[j+(1<<(i-1))][i-1]);
}
}
}
void solve(){
cin >> n;
for(int i=1;i<=n;i++) cin >> x[i];
buildupdown();
cin >> q;
for(int i=1;i<=q;i++){
int a;cin >> a;
cout << cal(a) << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
86 ms |
36528 KB |
Output is correct |
18 |
Correct |
83 ms |
36532 KB |
Output is correct |
19 |
Correct |
92 ms |
36476 KB |
Output is correct |
20 |
Correct |
85 ms |
36604 KB |
Output is correct |
21 |
Correct |
82 ms |
36620 KB |
Output is correct |
22 |
Correct |
86 ms |
36420 KB |
Output is correct |
23 |
Correct |
86 ms |
36588 KB |
Output is correct |
24 |
Correct |
85 ms |
36620 KB |
Output is correct |
25 |
Correct |
86 ms |
36448 KB |
Output is correct |
26 |
Correct |
80 ms |
36572 KB |
Output is correct |
27 |
Correct |
96 ms |
36520 KB |
Output is correct |
28 |
Correct |
89 ms |
36468 KB |
Output is correct |
29 |
Correct |
78 ms |
36556 KB |
Output is correct |
30 |
Correct |
86 ms |
36576 KB |
Output is correct |
31 |
Correct |
81 ms |
36500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
219 ms |
38560 KB |
Output is correct |
8 |
Correct |
237 ms |
38428 KB |
Output is correct |
9 |
Correct |
231 ms |
38464 KB |
Output is correct |
10 |
Correct |
264 ms |
38420 KB |
Output is correct |
11 |
Correct |
226 ms |
38476 KB |
Output is correct |
12 |
Correct |
215 ms |
38448 KB |
Output is correct |
13 |
Correct |
40 ms |
3976 KB |
Output is correct |
14 |
Correct |
31 ms |
1428 KB |
Output is correct |
15 |
Correct |
185 ms |
39616 KB |
Output is correct |
16 |
Correct |
197 ms |
39372 KB |
Output is correct |
17 |
Correct |
199 ms |
39420 KB |
Output is correct |
18 |
Correct |
44 ms |
3908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
86 ms |
36528 KB |
Output is correct |
18 |
Correct |
83 ms |
36532 KB |
Output is correct |
19 |
Correct |
92 ms |
36476 KB |
Output is correct |
20 |
Correct |
85 ms |
36604 KB |
Output is correct |
21 |
Correct |
82 ms |
36620 KB |
Output is correct |
22 |
Correct |
86 ms |
36420 KB |
Output is correct |
23 |
Correct |
86 ms |
36588 KB |
Output is correct |
24 |
Correct |
85 ms |
36620 KB |
Output is correct |
25 |
Correct |
86 ms |
36448 KB |
Output is correct |
26 |
Correct |
80 ms |
36572 KB |
Output is correct |
27 |
Correct |
96 ms |
36520 KB |
Output is correct |
28 |
Correct |
89 ms |
36468 KB |
Output is correct |
29 |
Correct |
78 ms |
36556 KB |
Output is correct |
30 |
Correct |
86 ms |
36576 KB |
Output is correct |
31 |
Correct |
81 ms |
36500 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
219 ms |
38560 KB |
Output is correct |
39 |
Correct |
237 ms |
38428 KB |
Output is correct |
40 |
Correct |
231 ms |
38464 KB |
Output is correct |
41 |
Correct |
264 ms |
38420 KB |
Output is correct |
42 |
Correct |
226 ms |
38476 KB |
Output is correct |
43 |
Correct |
215 ms |
38448 KB |
Output is correct |
44 |
Correct |
40 ms |
3976 KB |
Output is correct |
45 |
Correct |
31 ms |
1428 KB |
Output is correct |
46 |
Correct |
185 ms |
39616 KB |
Output is correct |
47 |
Correct |
197 ms |
39372 KB |
Output is correct |
48 |
Correct |
199 ms |
39420 KB |
Output is correct |
49 |
Correct |
44 ms |
3908 KB |
Output is correct |
50 |
Correct |
391 ms |
40732 KB |
Output is correct |
51 |
Correct |
399 ms |
40396 KB |
Output is correct |
52 |
Correct |
348 ms |
40756 KB |
Output is correct |
53 |
Correct |
249 ms |
40800 KB |
Output is correct |
54 |
Correct |
273 ms |
40780 KB |
Output is correct |
55 |
Correct |
245 ms |
40528 KB |
Output is correct |
56 |
Correct |
144 ms |
40520 KB |
Output is correct |
57 |
Correct |
152 ms |
40592 KB |
Output is correct |
58 |
Correct |
151 ms |
40372 KB |
Output is correct |