#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
// global
const int mxN = 2e5 + 1;
int ans[mxN];
vector<int> v;
int n;
void init() {
// init
}
void FindAnswer(int pos) {
if (pos == 0 || pos == n - 1) {
ans[pos] = v[n - 1] - v[0];
return;
}
int lb = pos, rb = pos;
ans[pos] = 0;
int cur, dist;
bool toleft;
if (v[pos] - v[pos - 1] <= v[pos + 1] - v[pos]) {
ans[pos] += v[pos] - v[pos - 1];
cur = v[pos - 1];
lb = pos - 1; rb = pos;
toleft = true;
} else {
ans[pos] += v[pos + 1] - v[pos];
cur = v[pos + 1];
lb = pos; rb = pos + 1;
toleft = false;
}
int lpos, rpos;
// cerr << "ANSWER " << pos << endl;
while (0 < lb && rb < n - 1) {
// cerr << "STATE " << lb << ' ' << rb << ' ' << ans[pos] << endl;
dist = v[rb] - v[lb];
if (toleft) {
lpos = lower_bound(v.begin(), v.end(), cur - dist) - v.begin();
if (lpos != lb) {
ans[pos] += cur - v[lpos];
cur = v[lpos];
lb = lpos;
continue;
}
} else {
rpos = upper_bound(v.begin(), v.end(), cur + dist) - v.begin() - 1;
if (rpos != rb) {
ans[pos] += v[rpos] - cur;
cur = v[rpos];
rb = rpos;
continue;
}
}
if (cur - v[lb - 1] <= v[rb + 1] - cur) {
ans[pos] += cur - v[lb - 1];
cur = v[lb - 1];
lb--;
toleft = true;
} else {
ans[pos] += v[rb + 1] - cur;
cur = v[rb + 1];
rb++;
toleft = false;
}
}
// cerr << "FINAL STATE " << lb << ' ' << rb << ' ' << ans[pos] << endl;
if (lb == 0) {
ans[pos] += v[n - 1] - cur;
} else if (rb == n - 1) {
ans[pos] += cur - v[0];
}
// cerr << "FINAL ANSWER " << pos << ": " << ans[pos] << endl;
return;
}
void solve() {
// solve
cin >> n;
v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) {
FindAnswer(i);
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
int pos = lower_bound(v.begin(), v.end(), x) - v.begin();
if (pos == 0) {
cout << (v[0] - x) + ans[0] << endl;
} else if (pos == n) {
cout << (x - v[pos - 1]) + ans[pos - 1] << endl;
} else {
// between v[pos - 1] and v[pos]
if ((x - v[pos - 1]) <= (v[pos] - x)) {
cout << (x - v[pos - 1]) + ans[pos - 1] << endl;
} else {
cout << (v[pos] - x) + ans[pos] << endl;
}
}
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
init(); solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
260 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
260 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
187 ms |
3344 KB |
Output is correct |
18 |
Correct |
194 ms |
3356 KB |
Output is correct |
19 |
Correct |
199 ms |
3444 KB |
Output is correct |
20 |
Correct |
219 ms |
3416 KB |
Output is correct |
21 |
Correct |
234 ms |
3440 KB |
Output is correct |
22 |
Correct |
232 ms |
3400 KB |
Output is correct |
23 |
Correct |
233 ms |
3408 KB |
Output is correct |
24 |
Correct |
302 ms |
3412 KB |
Output is correct |
25 |
Correct |
322 ms |
3540 KB |
Output is correct |
26 |
Correct |
290 ms |
3336 KB |
Output is correct |
27 |
Correct |
284 ms |
3552 KB |
Output is correct |
28 |
Correct |
299 ms |
3412 KB |
Output is correct |
29 |
Correct |
199 ms |
3336 KB |
Output is correct |
30 |
Correct |
181 ms |
3396 KB |
Output is correct |
31 |
Correct |
182 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
568 ms |
4756 KB |
Output is correct |
8 |
Correct |
525 ms |
4816 KB |
Output is correct |
9 |
Correct |
553 ms |
4748 KB |
Output is correct |
10 |
Correct |
551 ms |
4736 KB |
Output is correct |
11 |
Correct |
579 ms |
4828 KB |
Output is correct |
12 |
Correct |
555 ms |
4960 KB |
Output is correct |
13 |
Correct |
287 ms |
2160 KB |
Output is correct |
14 |
Correct |
248 ms |
888 KB |
Output is correct |
15 |
Correct |
597 ms |
5172 KB |
Output is correct |
16 |
Correct |
634 ms |
5324 KB |
Output is correct |
17 |
Correct |
603 ms |
5204 KB |
Output is correct |
18 |
Correct |
266 ms |
2156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
260 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
187 ms |
3344 KB |
Output is correct |
18 |
Correct |
194 ms |
3356 KB |
Output is correct |
19 |
Correct |
199 ms |
3444 KB |
Output is correct |
20 |
Correct |
219 ms |
3416 KB |
Output is correct |
21 |
Correct |
234 ms |
3440 KB |
Output is correct |
22 |
Correct |
232 ms |
3400 KB |
Output is correct |
23 |
Correct |
233 ms |
3408 KB |
Output is correct |
24 |
Correct |
302 ms |
3412 KB |
Output is correct |
25 |
Correct |
322 ms |
3540 KB |
Output is correct |
26 |
Correct |
290 ms |
3336 KB |
Output is correct |
27 |
Correct |
284 ms |
3552 KB |
Output is correct |
28 |
Correct |
299 ms |
3412 KB |
Output is correct |
29 |
Correct |
199 ms |
3336 KB |
Output is correct |
30 |
Correct |
181 ms |
3396 KB |
Output is correct |
31 |
Correct |
182 ms |
3408 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
568 ms |
4756 KB |
Output is correct |
39 |
Correct |
525 ms |
4816 KB |
Output is correct |
40 |
Correct |
553 ms |
4748 KB |
Output is correct |
41 |
Correct |
551 ms |
4736 KB |
Output is correct |
42 |
Correct |
579 ms |
4828 KB |
Output is correct |
43 |
Correct |
555 ms |
4960 KB |
Output is correct |
44 |
Correct |
287 ms |
2160 KB |
Output is correct |
45 |
Correct |
248 ms |
888 KB |
Output is correct |
46 |
Correct |
597 ms |
5172 KB |
Output is correct |
47 |
Correct |
634 ms |
5324 KB |
Output is correct |
48 |
Correct |
603 ms |
5204 KB |
Output is correct |
49 |
Correct |
266 ms |
2156 KB |
Output is correct |
50 |
Correct |
520 ms |
5544 KB |
Output is correct |
51 |
Correct |
505 ms |
5580 KB |
Output is correct |
52 |
Correct |
541 ms |
5596 KB |
Output is correct |
53 |
Correct |
628 ms |
5544 KB |
Output is correct |
54 |
Correct |
570 ms |
5596 KB |
Output is correct |
55 |
Correct |
616 ms |
5828 KB |
Output is correct |
56 |
Correct |
478 ms |
5452 KB |
Output is correct |
57 |
Correct |
461 ms |
5520 KB |
Output is correct |
58 |
Correct |
481 ms |
5648 KB |
Output is correct |