#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 2e5 + 100;
ll p[N], dis[N];
ll sp[N][20], n;
void build() {
for (int i = 2; i <= n; i++) {
dis[i] = p[i] - p[i - 1];
sp[i][0] = dis[i];
}
for (int j = 1; j < 20; j++) {
for (int i = 1; i + (1 << j) <= n + 1; i++) {
sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
}
int get(int l, int r) {
l++;
if (l > r)
return -1;
int t = log2(r - l + 1);
return max(sp[l][t], sp[r - (1 << t) + 1][t]);
}
ll get(int l, int r, int x) {
ll ans = 0;
while (l >= 1 || r <= n) {
if (r > n) {
ans += abs(x - p[1]);
break;
}
if (l < 1) {
ans += abs(x - p[n]);
break;
}
ll dis = max(abs(p[r] - x), abs(p[l] - x));
if (abs(p[l] - x) <= abs(p[r] - x)) {
int nw = 0, l1 = 1, r1 = l;
while (l1 <= r1) {
int m = (l1 + r1) / 2;
if (get(m, l) <= dis) {
nw = m;
r1 = m - 1;
} else {
l1 = m + 1;
}
}
ans += abs(x - p[nw]);
x = p[nw];
l = nw - 1;
} else {
int nw = 0, l1 = r, r1 = n;
while (l1 <= r1) {
int m = (l1 + r1) / 2;
if (get(r, m) < dis) {
nw = m;
l1 = m + 1;
} else {
r1 = m - 1;
}
}
ans += abs(x - p[nw]);
x = p[nw];
r = nw + 1;
}
}
return ans;
}
ll solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
build();
int q;
cin >> q;
for (int i = 1; i <= q; i++) {
ll x;
cin >> x;
int r = lower_bound(p + 1, p + 1 + n, x) - p, l = r - 1;
cout << get(l, r, x) << '\n';
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
//cout << solve() << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4868 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4868 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
58 ms |
35692 KB |
Output is correct |
18 |
Correct |
56 ms |
36824 KB |
Output is correct |
19 |
Correct |
55 ms |
36804 KB |
Output is correct |
20 |
Correct |
55 ms |
36692 KB |
Output is correct |
21 |
Correct |
53 ms |
36824 KB |
Output is correct |
22 |
Correct |
58 ms |
36792 KB |
Output is correct |
23 |
Correct |
62 ms |
36692 KB |
Output is correct |
24 |
Correct |
57 ms |
36908 KB |
Output is correct |
25 |
Correct |
60 ms |
36828 KB |
Output is correct |
26 |
Correct |
53 ms |
36692 KB |
Output is correct |
27 |
Correct |
66 ms |
36652 KB |
Output is correct |
28 |
Correct |
61 ms |
36832 KB |
Output is correct |
29 |
Correct |
60 ms |
36772 KB |
Output is correct |
30 |
Correct |
59 ms |
36772 KB |
Output is correct |
31 |
Correct |
61 ms |
36948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
427 ms |
38092 KB |
Output is correct |
8 |
Correct |
468 ms |
38672 KB |
Output is correct |
9 |
Correct |
409 ms |
38732 KB |
Output is correct |
10 |
Correct |
438 ms |
39028 KB |
Output is correct |
11 |
Correct |
429 ms |
38740 KB |
Output is correct |
12 |
Correct |
443 ms |
38820 KB |
Output is correct |
13 |
Correct |
33 ms |
6228 KB |
Output is correct |
14 |
Correct |
24 ms |
3548 KB |
Output is correct |
15 |
Correct |
464 ms |
39876 KB |
Output is correct |
16 |
Correct |
461 ms |
39812 KB |
Output is correct |
17 |
Correct |
431 ms |
39904 KB |
Output is correct |
18 |
Correct |
32 ms |
6232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4868 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
58 ms |
35692 KB |
Output is correct |
18 |
Correct |
56 ms |
36824 KB |
Output is correct |
19 |
Correct |
55 ms |
36804 KB |
Output is correct |
20 |
Correct |
55 ms |
36692 KB |
Output is correct |
21 |
Correct |
53 ms |
36824 KB |
Output is correct |
22 |
Correct |
58 ms |
36792 KB |
Output is correct |
23 |
Correct |
62 ms |
36692 KB |
Output is correct |
24 |
Correct |
57 ms |
36908 KB |
Output is correct |
25 |
Correct |
60 ms |
36828 KB |
Output is correct |
26 |
Correct |
53 ms |
36692 KB |
Output is correct |
27 |
Correct |
66 ms |
36652 KB |
Output is correct |
28 |
Correct |
61 ms |
36832 KB |
Output is correct |
29 |
Correct |
60 ms |
36772 KB |
Output is correct |
30 |
Correct |
59 ms |
36772 KB |
Output is correct |
31 |
Correct |
61 ms |
36948 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
427 ms |
38092 KB |
Output is correct |
39 |
Correct |
468 ms |
38672 KB |
Output is correct |
40 |
Correct |
409 ms |
38732 KB |
Output is correct |
41 |
Correct |
438 ms |
39028 KB |
Output is correct |
42 |
Correct |
429 ms |
38740 KB |
Output is correct |
43 |
Correct |
443 ms |
38820 KB |
Output is correct |
44 |
Correct |
33 ms |
6228 KB |
Output is correct |
45 |
Correct |
24 ms |
3548 KB |
Output is correct |
46 |
Correct |
464 ms |
39876 KB |
Output is correct |
47 |
Correct |
461 ms |
39812 KB |
Output is correct |
48 |
Correct |
431 ms |
39904 KB |
Output is correct |
49 |
Correct |
32 ms |
6232 KB |
Output is correct |
50 |
Correct |
1391 ms |
40828 KB |
Output is correct |
51 |
Correct |
1344 ms |
40916 KB |
Output is correct |
52 |
Correct |
1192 ms |
40868 KB |
Output is correct |
53 |
Correct |
1242 ms |
41032 KB |
Output is correct |
54 |
Correct |
1219 ms |
40900 KB |
Output is correct |
55 |
Correct |
1179 ms |
40900 KB |
Output is correct |
56 |
Correct |
156 ms |
40752 KB |
Output is correct |
57 |
Correct |
162 ms |
40788 KB |
Output is correct |
58 |
Correct |
213 ms |
40624 KB |
Output is correct |