#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
const long long inf=1e15;
long long n,t[MAXN],q,s;
long long solve(long long s,int from,int to){
int l=from,r=to;
long long res=0,gap=0;
while(l!=0 or r!=n+1){
if(l>=1 and s-t[l]<=t[r]-s){
int ll=0,rr=l,mid;
while(ll+1<rr){
mid=(ll+rr)/2;
if(s-t[mid]<gap){
rr=mid;
}else{
ll=mid;
}
}
res+=s-t[rr];
l=rr-1; s=t[rr];
}else if(r<=n and t[r]-s<=s-t[l]){
int ll=r,rr=n+1,mid;
while(ll+1<rr){
mid=(ll+rr)/2;
if(t[mid]-s<gap){
ll=mid;
}else{
rr=mid;
}
}
res+=t[ll]-s;
r=ll+1; s=t[ll];
}else{
exit(0);
}
gap=t[r-1]-t[l+1];
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>t[i];
}
t[n+1]=inf;
t[0]=-inf;
cin>>q;
for(int i=1;i<=q;i++){
cin>>s;
int l=0,r=n+1,mid;
while(l+1<r){
mid=(l+r)/2;
if(t[mid]<=s){
l=mid;
}else{
r=mid;
}
}
cout<<solve(s,l,l+1)<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 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 |
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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 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 |
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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
14 ms |
1884 KB |
Output is correct |
19 |
Correct |
15 ms |
1828 KB |
Output is correct |
20 |
Correct |
18 ms |
1868 KB |
Output is correct |
21 |
Correct |
14 ms |
1884 KB |
Output is correct |
22 |
Correct |
16 ms |
1800 KB |
Output is correct |
23 |
Correct |
15 ms |
1884 KB |
Output is correct |
24 |
Correct |
15 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1848 KB |
Output is correct |
26 |
Correct |
15 ms |
2424 KB |
Output is correct |
27 |
Correct |
15 ms |
1884 KB |
Output is correct |
28 |
Correct |
14 ms |
1880 KB |
Output is correct |
29 |
Correct |
14 ms |
1884 KB |
Output is correct |
30 |
Correct |
14 ms |
1884 KB |
Output is correct |
31 |
Correct |
22 ms |
1916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
448 ms |
4260 KB |
Output is correct |
8 |
Correct |
410 ms |
5848 KB |
Output is correct |
9 |
Correct |
423 ms |
5772 KB |
Output is correct |
10 |
Correct |
417 ms |
5712 KB |
Output is correct |
11 |
Correct |
413 ms |
5908 KB |
Output is correct |
12 |
Correct |
417 ms |
5916 KB |
Output is correct |
13 |
Correct |
37 ms |
4296 KB |
Output is correct |
14 |
Correct |
24 ms |
1616 KB |
Output is correct |
15 |
Correct |
326 ms |
6932 KB |
Output is correct |
16 |
Correct |
381 ms |
6776 KB |
Output is correct |
17 |
Correct |
319 ms |
6872 KB |
Output is correct |
18 |
Correct |
37 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 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 |
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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
15 ms |
1884 KB |
Output is correct |
18 |
Correct |
14 ms |
1884 KB |
Output is correct |
19 |
Correct |
15 ms |
1828 KB |
Output is correct |
20 |
Correct |
18 ms |
1868 KB |
Output is correct |
21 |
Correct |
14 ms |
1884 KB |
Output is correct |
22 |
Correct |
16 ms |
1800 KB |
Output is correct |
23 |
Correct |
15 ms |
1884 KB |
Output is correct |
24 |
Correct |
15 ms |
1884 KB |
Output is correct |
25 |
Correct |
15 ms |
1848 KB |
Output is correct |
26 |
Correct |
15 ms |
2424 KB |
Output is correct |
27 |
Correct |
15 ms |
1884 KB |
Output is correct |
28 |
Correct |
14 ms |
1880 KB |
Output is correct |
29 |
Correct |
14 ms |
1884 KB |
Output is correct |
30 |
Correct |
14 ms |
1884 KB |
Output is correct |
31 |
Correct |
22 ms |
1916 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 |
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 |
448 ms |
4260 KB |
Output is correct |
39 |
Correct |
410 ms |
5848 KB |
Output is correct |
40 |
Correct |
423 ms |
5772 KB |
Output is correct |
41 |
Correct |
417 ms |
5712 KB |
Output is correct |
42 |
Correct |
413 ms |
5908 KB |
Output is correct |
43 |
Correct |
417 ms |
5916 KB |
Output is correct |
44 |
Correct |
37 ms |
4296 KB |
Output is correct |
45 |
Correct |
24 ms |
1616 KB |
Output is correct |
46 |
Correct |
326 ms |
6932 KB |
Output is correct |
47 |
Correct |
381 ms |
6776 KB |
Output is correct |
48 |
Correct |
319 ms |
6872 KB |
Output is correct |
49 |
Correct |
37 ms |
4188 KB |
Output is correct |
50 |
Correct |
511 ms |
8004 KB |
Output is correct |
51 |
Correct |
491 ms |
8044 KB |
Output is correct |
52 |
Correct |
377 ms |
8128 KB |
Output is correct |
53 |
Correct |
467 ms |
8020 KB |
Output is correct |
54 |
Correct |
461 ms |
7872 KB |
Output is correct |
55 |
Correct |
469 ms |
8072 KB |
Output is correct |
56 |
Correct |
295 ms |
7876 KB |
Output is correct |
57 |
Correct |
276 ms |
7720 KB |
Output is correct |
58 |
Correct |
313 ms |
7936 KB |
Output is correct |