#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
using namespace std;
const int N = 2e5 + 5,mod = 1e9 + 7;
int a[N],t[5][4*N];
void build(int h,int l,int r){
if (l == r){
t[1][h] = 2*a[l] - a[l + 1];
t[0][h] = 2*a[l] - a[l - 1];
return;
}
build(left);
build(right);
t[1][h] = min(t[1][h*2],t[1][h*2+1]);
t[0][h] = max(t[0][h*2],t[0][h*2+1]);
}
int getr(int h,int l,int r,int L,int R,int v){
if (r < L || R < l) return -1;
if (L <= l && r <= R) {
if (t[1][h] > v) return -1;
if (l == r) return l;
if (t[1][h*2] <= v) return getr(left,L,R,v);
return getr(right,L,R,v);
}
int y = getr(left,L,R,v);
if (y != -1) return y;
return getr(right,L,R,v);
}
int getl(int h,int l,int r,int L,int R,int v){
if (r < L || R < l) return -1;
if (L <= l && r <= R) {
if (t[0][h] <= v) return -1;
if (l == r) return l;
if (t[0][h*2+1] > v) return getl(right,L,R,v);
return getl(left,L,R,v);
}
int y = getl(right,L,R,v);
if (y != -1) return y;
return getl(left,L,R,v);
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
build(1,1,n);
int q;
cin>>q;
while (q--){
int s;
cin>>s;
int le = 1,ri = n,R = n + 1;
while (le <= ri){
int mid = (le + ri) >> 1;
if (a[mid] > s) {
R = mid;
ri = mid - 1;
}else{
le = mid + 1;
}
}
le = 1; ri = n; int L = 0;
while (le <= ri){
int mid = (le + ri) >> 1;
if (a[mid] < s){
L = mid;
le = mid + 1;
}else{
ri = mid - 1;
}
}
int ans=0;
if (L == 0){
ans += (a[n] - s);
cout<<ans<<endl;
continue;
}else if (R == n + 1){
ans += (s - a[1]);
cout<<ans<<endl;
continue;
}
int dir = 1;
//dir == 1 -> right
//dir == 0 <- left
if (a[R] - s < s - a[L]){
dir = 1;
ans += (a[R] - s);
s = a[R];
}else{
dir = 0;
ans += (s - a[L]);
s = a[L];
}
while (L != 0 && R != n + 1){
if (dir == 1){
// vedzebt R -ze meti an tol umcires r-s romlistvisac
//2*x[r] - x[r + 1] <= x[L]
int r = getr(1,1,n,R,n,a[L]);
if (r == -1) r = n;
ans += a[r] - s;
ans += a[r] - a[L];
s = a[L];
R = r + 1;
}else{
//vedzebt L-ze naklebi an tol udides l-s romlistvisac
//2*x[l] - x[l - 1] > x[R]
int l = getl(1,1,n,1,L,a[R]);
if (l == -1) l = 1;
ans += s - a[l];
ans += a[R] - a[l];
s = a[R];
L = l - 1;
}
dir ^= 1;
}
if (L == 0){
ans += (a[n] - s);
}else if (R == n + 1){
ans += (s - a[1]);
}
cout<<ans<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
14 ms |
6232 KB |
Output is correct |
18 |
Correct |
15 ms |
6236 KB |
Output is correct |
19 |
Correct |
14 ms |
7388 KB |
Output is correct |
20 |
Correct |
18 ms |
6232 KB |
Output is correct |
21 |
Correct |
15 ms |
6196 KB |
Output is correct |
22 |
Correct |
15 ms |
6156 KB |
Output is correct |
23 |
Correct |
14 ms |
6236 KB |
Output is correct |
24 |
Correct |
15 ms |
6236 KB |
Output is correct |
25 |
Correct |
15 ms |
6236 KB |
Output is correct |
26 |
Correct |
19 ms |
6080 KB |
Output is correct |
27 |
Correct |
15 ms |
6236 KB |
Output is correct |
28 |
Correct |
23 ms |
7392 KB |
Output is correct |
29 |
Correct |
24 ms |
6236 KB |
Output is correct |
30 |
Correct |
14 ms |
6284 KB |
Output is correct |
31 |
Correct |
15 ms |
6084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
308 ms |
7604 KB |
Output is correct |
8 |
Correct |
352 ms |
8788 KB |
Output is correct |
9 |
Correct |
313 ms |
8952 KB |
Output is correct |
10 |
Correct |
286 ms |
8856 KB |
Output is correct |
11 |
Correct |
309 ms |
8820 KB |
Output is correct |
12 |
Correct |
285 ms |
7760 KB |
Output is correct |
13 |
Correct |
217 ms |
4436 KB |
Output is correct |
14 |
Correct |
212 ms |
2900 KB |
Output is correct |
15 |
Correct |
275 ms |
8020 KB |
Output is correct |
16 |
Correct |
294 ms |
12444 KB |
Output is correct |
17 |
Correct |
340 ms |
12444 KB |
Output is correct |
18 |
Correct |
225 ms |
6224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
14 ms |
6232 KB |
Output is correct |
18 |
Correct |
15 ms |
6236 KB |
Output is correct |
19 |
Correct |
14 ms |
7388 KB |
Output is correct |
20 |
Correct |
18 ms |
6232 KB |
Output is correct |
21 |
Correct |
15 ms |
6196 KB |
Output is correct |
22 |
Correct |
15 ms |
6156 KB |
Output is correct |
23 |
Correct |
14 ms |
6236 KB |
Output is correct |
24 |
Correct |
15 ms |
6236 KB |
Output is correct |
25 |
Correct |
15 ms |
6236 KB |
Output is correct |
26 |
Correct |
19 ms |
6080 KB |
Output is correct |
27 |
Correct |
15 ms |
6236 KB |
Output is correct |
28 |
Correct |
23 ms |
7392 KB |
Output is correct |
29 |
Correct |
24 ms |
6236 KB |
Output is correct |
30 |
Correct |
14 ms |
6284 KB |
Output is correct |
31 |
Correct |
15 ms |
6084 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
308 ms |
7604 KB |
Output is correct |
39 |
Correct |
352 ms |
8788 KB |
Output is correct |
40 |
Correct |
313 ms |
8952 KB |
Output is correct |
41 |
Correct |
286 ms |
8856 KB |
Output is correct |
42 |
Correct |
309 ms |
8820 KB |
Output is correct |
43 |
Correct |
285 ms |
7760 KB |
Output is correct |
44 |
Correct |
217 ms |
4436 KB |
Output is correct |
45 |
Correct |
212 ms |
2900 KB |
Output is correct |
46 |
Correct |
275 ms |
8020 KB |
Output is correct |
47 |
Correct |
294 ms |
12444 KB |
Output is correct |
48 |
Correct |
340 ms |
12444 KB |
Output is correct |
49 |
Correct |
225 ms |
6224 KB |
Output is correct |
50 |
Correct |
356 ms |
12452 KB |
Output is correct |
51 |
Correct |
364 ms |
12464 KB |
Output is correct |
52 |
Correct |
368 ms |
12424 KB |
Output is correct |
53 |
Correct |
370 ms |
12368 KB |
Output is correct |
54 |
Correct |
354 ms |
12372 KB |
Output is correct |
55 |
Correct |
363 ms |
12116 KB |
Output is correct |
56 |
Correct |
247 ms |
12368 KB |
Output is correct |
57 |
Correct |
257 ms |
13400 KB |
Output is correct |
58 |
Correct |
260 ms |
12316 KB |
Output is correct |