#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;
int N,Q;
ll X[MX];
ll val[MX], LOG[MX];
struct ST1 {
ll st[MX][20];
int que(int l, int r) {
int lg=LOG[r-l+1];
return min(st[l][lg],st[r-(1<<lg)+1][lg]);
}
void prep() {
for(int i=1;i<N;i++) st[i][0]=2*X[i]-X[i+1];
st[N][0]=-2e9;
for(int k=1;k<20;k++) {
for(int i=1;i+(1<<k)<=N;i++) {
st[i][k]=min(st[i][k-1],st[i+(1<<(k-1))][k-1]);
}
}
}
} st;
struct ST2 {
ll st[MX][20];
int que(int l, int r) {
int lg=LOG[r-l+1];
return max(st[l][lg],st[r-(1<<lg)+1][lg]);
}
void prep() {
for(int i=2;i<=N;i++) st[i][0]=2*X[i]-X[i-1];
st[1][0]=2e9;
for(int k=1;k<20;k++) {
for(int i=1;i+(1<<k)<=N;i++) {
st[i][k]=max(st[i][k-1],st[i+(1<<(k-1))][k-1]);
}
}
}
} tt;
int S[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
LOG[1]=0;
for(int i=2;i<MX;i++) LOG[i]=LOG[i/2]+1;
cin>>N;
for(int i=1;i<=N;i++) cin>>X[i];
X[0]=-2e9;
X[N+1]=2e9;
cin>>Q;
for(int i=1;i<=Q;i++) cin>>S[i];
st.prep();
tt.prep();
for(int i=1;i<=N;i++) {
if(N==1) break;
int p=i;
int j=i;
if(i==1) {
val[p]+=X[i+1]-X[i];
j+=1;
} else if(i==N) {
val[p]+=X[i]-X[i-1];
j-=1;
} else {
if(X[i]-X[i-1]<=X[i+1]-X[i]) {
val[p]+=X[i]-X[i-1];
j-=1;
} else {
val[p]+=X[i+1]-X[i];
j+=1;
}
}
assert(min(i,j)>=1 && max(i,j)<=N);
while(!(min(i,j)==1 && max(i,j)==N)) {
if(i<j) {
int l=j,r=N-1,res=-1;
while(l<=r) {
int mid=(l+r)/2;
if(st.que(j,mid)>X[i-1]) {
res=mid,l=mid+1;
} else {
r=mid-1;
}
}
if(res==-1) {
val[p]+=X[j]-X[i];
swap(i,j);
} else {
res+=1;
val[p]+=X[res]-X[j];
j=res;
}
} else { // j < i
int l=2,r=j,res=-1;
while(l<=r) {
int mid=(l+r)/2;
if(tt.que(mid,j)<=X[i+1]) {
res=mid,r=mid-1;
} else {
l=mid+1;
}
}
if(res==-1) {
val[p]+=X[i]-X[j];
swap(i,j);
} else {
res-=1;
val[p]+=X[j]-X[res];
j=res;
}
}
}
i=p;
}
for(int i=1;i<=Q;i++) {
int x=S[i];
auto it=lower_bound(X+1,X+N+1,x)-X;
if(X[it]-x<x-X[it-1]) {
cout<<val[it]+X[it]-x<<'\n';
} else {
cout<<val[it-1]+x-X[it-1]<<'\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10712 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10712 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
208 ms |
68432 KB |
Output is correct |
18 |
Correct |
184 ms |
68356 KB |
Output is correct |
19 |
Correct |
179 ms |
68180 KB |
Output is correct |
20 |
Correct |
180 ms |
68180 KB |
Output is correct |
21 |
Correct |
181 ms |
68184 KB |
Output is correct |
22 |
Correct |
186 ms |
68312 KB |
Output is correct |
23 |
Correct |
183 ms |
68312 KB |
Output is correct |
24 |
Correct |
136 ms |
68364 KB |
Output is correct |
25 |
Correct |
140 ms |
68360 KB |
Output is correct |
26 |
Correct |
136 ms |
68176 KB |
Output is correct |
27 |
Correct |
133 ms |
68176 KB |
Output is correct |
28 |
Correct |
146 ms |
68316 KB |
Output is correct |
29 |
Correct |
278 ms |
68172 KB |
Output is correct |
30 |
Correct |
305 ms |
70188 KB |
Output is correct |
31 |
Correct |
270 ms |
70240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
165 ms |
69780 KB |
Output is correct |
8 |
Correct |
148 ms |
69716 KB |
Output is correct |
9 |
Correct |
149 ms |
69816 KB |
Output is correct |
10 |
Correct |
148 ms |
69708 KB |
Output is correct |
11 |
Correct |
142 ms |
69720 KB |
Output is correct |
12 |
Correct |
141 ms |
69712 KB |
Output is correct |
13 |
Correct |
29 ms |
12804 KB |
Output is correct |
14 |
Correct |
22 ms |
11356 KB |
Output is correct |
15 |
Correct |
174 ms |
70336 KB |
Output is correct |
16 |
Correct |
160 ms |
70340 KB |
Output is correct |
17 |
Correct |
172 ms |
70224 KB |
Output is correct |
18 |
Correct |
30 ms |
12628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10712 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
208 ms |
68432 KB |
Output is correct |
18 |
Correct |
184 ms |
68356 KB |
Output is correct |
19 |
Correct |
179 ms |
68180 KB |
Output is correct |
20 |
Correct |
180 ms |
68180 KB |
Output is correct |
21 |
Correct |
181 ms |
68184 KB |
Output is correct |
22 |
Correct |
186 ms |
68312 KB |
Output is correct |
23 |
Correct |
183 ms |
68312 KB |
Output is correct |
24 |
Correct |
136 ms |
68364 KB |
Output is correct |
25 |
Correct |
140 ms |
68360 KB |
Output is correct |
26 |
Correct |
136 ms |
68176 KB |
Output is correct |
27 |
Correct |
133 ms |
68176 KB |
Output is correct |
28 |
Correct |
146 ms |
68316 KB |
Output is correct |
29 |
Correct |
278 ms |
68172 KB |
Output is correct |
30 |
Correct |
305 ms |
70188 KB |
Output is correct |
31 |
Correct |
270 ms |
70240 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10584 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
165 ms |
69780 KB |
Output is correct |
39 |
Correct |
148 ms |
69716 KB |
Output is correct |
40 |
Correct |
149 ms |
69816 KB |
Output is correct |
41 |
Correct |
148 ms |
69708 KB |
Output is correct |
42 |
Correct |
142 ms |
69720 KB |
Output is correct |
43 |
Correct |
141 ms |
69712 KB |
Output is correct |
44 |
Correct |
29 ms |
12804 KB |
Output is correct |
45 |
Correct |
22 ms |
11356 KB |
Output is correct |
46 |
Correct |
174 ms |
70336 KB |
Output is correct |
47 |
Correct |
160 ms |
70340 KB |
Output is correct |
48 |
Correct |
172 ms |
70224 KB |
Output is correct |
49 |
Correct |
30 ms |
12628 KB |
Output is correct |
50 |
Correct |
247 ms |
74456 KB |
Output is correct |
51 |
Correct |
277 ms |
74432 KB |
Output is correct |
52 |
Correct |
254 ms |
74772 KB |
Output is correct |
53 |
Correct |
183 ms |
74580 KB |
Output is correct |
54 |
Correct |
202 ms |
74540 KB |
Output is correct |
55 |
Correct |
195 ms |
74576 KB |
Output is correct |
56 |
Correct |
322 ms |
74404 KB |
Output is correct |
57 |
Correct |
305 ms |
74352 KB |
Output is correct |
58 |
Correct |
349 ms |
74468 KB |
Output is correct |