#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node {
int val, lpt, rpt;
};
const int maxn = 2e5 + 5, maxN = 8e5 + 5, INF = 1e18;
vector<node> seg[maxN][2];
int def[2] = {INF, 0};
void build(int id, int l, int r, int i) {
seg[id][i] = {{def[i], 0, 0}};
if (l==r) return;
int mid = (l+r)/2;
build(id*2, l, mid, i); build(id*2+1, mid+1, r, i);
}
void update(int id, int l, int r, int target, int i) {
if (r<target || target<l) return;
if (l==r) {
seg[id][i].push_back({l, 0, 0});
return;
}
int mid = (l+r)/2;
update(id*2, l, mid, target, i); update(id*2+1, mid+1, r, target, i);
if (i==0) {
seg[id][i].push_back({min(seg[id*2][i].back().val, seg[id*2+1][i].back().val), (int)seg[id*2][i].size()-1, (int)seg[id*2+1][i].size()-1});
} else {
seg[id][i].push_back({max(seg[id*2][i].back().val, seg[id*2+1][i].back().val), (int)seg[id*2][i].size()-1, (int)seg[id*2+1][i].size()-1});
}
}
int qry(int id, int pt, int l, int r, int findl, int findr, int i) {
if (r<findl || findr<l) return def[i];
// cout << l << " " << r << " " << pt << " " << seg[id][i][pt].val << endl;
if (findl<=l && r<=findr) return seg[id][i][pt].val;
int mid = (l+r)/2;
int lhs = qry(id*2, seg[id][i][pt].lpt, l, mid, findl, findr, i);
int rhs = qry(id*2+1, seg[id][i][pt].rpt, mid+1, r, findl, findr, i);
if (i==0) return min(lhs, rhs);
else return max(lhs, rhs);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
int a[n+2];
a[0] = -INF, a[n+1] = INF;
for (int i=1;i<=n;i++) cin >> a[i];
build(1, 1, n, 0); build(1, 1, n, 1);
vector<pair<int,int>> R = {{-INF, -INF}};
for (int i=1;i<=n;i++) R.push_back({2*a[i] - a[i+1], i});
sort(R.begin()+1, R.end());
for (int i=1;i<=n;i++) {
update(1, 1, n, R[i].second, 0);
// cout << R[i].second << " " << seg[1][0].back().val << endl;
}
vector<pair<int,int>> L = {{-INF, INF}};
for (int i=1;i<=n;i++) L.push_back({-(2*a[i] - a[i-1]), i});
sort(L.begin()+1, L.end());
for (int i=1;i<=n;i++) update(1, 1, n, L[i].second, 1);
// for (int i=1;i<=n;i++) {
// for (int j=1;j<=n;j++) cout << qry(1, i, 1, n, j, j, 1) << " "; cout << endl;
// }
// return 0;
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
int pos = lower_bound(a+1, a+n+1, x) - a;
if (x - a[pos-1] <= a[pos] - x) pos--;
int ans = abs(x-a[pos]), l = pos, r = pos;
bool state = 0;
while (true) {
if (l==1 || r==n) {
ans += a[n] - a[1];
break;
}
if (state==0) {
int id = upper_bound(R.begin()+1, R.end(), make_pair(a[l-1], INF)) - R.begin() - 1;
int nxt = qry(1, id, 1, n, r, n, 0);
ans += a[nxt] - a[l];
r = nxt;
} else if (state==1) {
int id = lower_bound(L.begin()+1, L.end(), make_pair(-a[r+1], -INF)) - L.begin() - 1;
int nxt = qry(1, id, 1, n, 1, l, 1);
ans += a[r] - a[nxt];
l = nxt;
}
state = !state;
}
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
37980 KB |
Output is correct |
2 |
Correct |
18 ms |
39768 KB |
Output is correct |
3 |
Correct |
20 ms |
39852 KB |
Output is correct |
4 |
Correct |
17 ms |
39772 KB |
Output is correct |
5 |
Correct |
18 ms |
39772 KB |
Output is correct |
6 |
Correct |
18 ms |
39644 KB |
Output is correct |
7 |
Correct |
18 ms |
39640 KB |
Output is correct |
8 |
Correct |
16 ms |
37980 KB |
Output is correct |
9 |
Correct |
18 ms |
37832 KB |
Output is correct |
10 |
Correct |
15 ms |
37980 KB |
Output is correct |
11 |
Correct |
15 ms |
37980 KB |
Output is correct |
12 |
Correct |
14 ms |
37980 KB |
Output is correct |
13 |
Correct |
14 ms |
37980 KB |
Output is correct |
14 |
Correct |
18 ms |
39772 KB |
Output is correct |
15 |
Correct |
18 ms |
39772 KB |
Output is correct |
16 |
Correct |
17 ms |
39772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
37980 KB |
Output is correct |
2 |
Correct |
18 ms |
39768 KB |
Output is correct |
3 |
Correct |
20 ms |
39852 KB |
Output is correct |
4 |
Correct |
17 ms |
39772 KB |
Output is correct |
5 |
Correct |
18 ms |
39772 KB |
Output is correct |
6 |
Correct |
18 ms |
39644 KB |
Output is correct |
7 |
Correct |
18 ms |
39640 KB |
Output is correct |
8 |
Correct |
16 ms |
37980 KB |
Output is correct |
9 |
Correct |
18 ms |
37832 KB |
Output is correct |
10 |
Correct |
15 ms |
37980 KB |
Output is correct |
11 |
Correct |
15 ms |
37980 KB |
Output is correct |
12 |
Correct |
14 ms |
37980 KB |
Output is correct |
13 |
Correct |
14 ms |
37980 KB |
Output is correct |
14 |
Correct |
18 ms |
39772 KB |
Output is correct |
15 |
Correct |
18 ms |
39772 KB |
Output is correct |
16 |
Correct |
17 ms |
39772 KB |
Output is correct |
17 |
Correct |
424 ms |
284268 KB |
Output is correct |
18 |
Correct |
415 ms |
284076 KB |
Output is correct |
19 |
Correct |
415 ms |
284012 KB |
Output is correct |
20 |
Correct |
413 ms |
284128 KB |
Output is correct |
21 |
Correct |
464 ms |
283616 KB |
Output is correct |
22 |
Correct |
415 ms |
283564 KB |
Output is correct |
23 |
Correct |
436 ms |
283636 KB |
Output is correct |
24 |
Correct |
416 ms |
284096 KB |
Output is correct |
25 |
Correct |
415 ms |
284264 KB |
Output is correct |
26 |
Correct |
438 ms |
283988 KB |
Output is correct |
27 |
Correct |
417 ms |
284120 KB |
Output is correct |
28 |
Correct |
428 ms |
284072 KB |
Output is correct |
29 |
Correct |
422 ms |
284844 KB |
Output is correct |
30 |
Correct |
430 ms |
284840 KB |
Output is correct |
31 |
Correct |
433 ms |
284664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
37976 KB |
Output is correct |
2 |
Correct |
19 ms |
37980 KB |
Output is correct |
3 |
Correct |
16 ms |
37980 KB |
Output is correct |
4 |
Correct |
14 ms |
37980 KB |
Output is correct |
5 |
Correct |
15 ms |
37980 KB |
Output is correct |
6 |
Correct |
15 ms |
37980 KB |
Output is correct |
7 |
Correct |
1541 ms |
286080 KB |
Output is correct |
8 |
Correct |
1540 ms |
286124 KB |
Output is correct |
9 |
Correct |
1504 ms |
286168 KB |
Output is correct |
10 |
Correct |
1505 ms |
285988 KB |
Output is correct |
11 |
Correct |
1555 ms |
286136 KB |
Output is correct |
12 |
Correct |
1476 ms |
286124 KB |
Output is correct |
13 |
Correct |
241 ms |
41808 KB |
Output is correct |
14 |
Correct |
197 ms |
38992 KB |
Output is correct |
15 |
Correct |
1040 ms |
287280 KB |
Output is correct |
16 |
Correct |
1034 ms |
287240 KB |
Output is correct |
17 |
Correct |
1054 ms |
287204 KB |
Output is correct |
18 |
Correct |
216 ms |
41812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
37980 KB |
Output is correct |
2 |
Correct |
18 ms |
39768 KB |
Output is correct |
3 |
Correct |
20 ms |
39852 KB |
Output is correct |
4 |
Correct |
17 ms |
39772 KB |
Output is correct |
5 |
Correct |
18 ms |
39772 KB |
Output is correct |
6 |
Correct |
18 ms |
39644 KB |
Output is correct |
7 |
Correct |
18 ms |
39640 KB |
Output is correct |
8 |
Correct |
16 ms |
37980 KB |
Output is correct |
9 |
Correct |
18 ms |
37832 KB |
Output is correct |
10 |
Correct |
15 ms |
37980 KB |
Output is correct |
11 |
Correct |
15 ms |
37980 KB |
Output is correct |
12 |
Correct |
14 ms |
37980 KB |
Output is correct |
13 |
Correct |
14 ms |
37980 KB |
Output is correct |
14 |
Correct |
18 ms |
39772 KB |
Output is correct |
15 |
Correct |
18 ms |
39772 KB |
Output is correct |
16 |
Correct |
17 ms |
39772 KB |
Output is correct |
17 |
Correct |
424 ms |
284268 KB |
Output is correct |
18 |
Correct |
415 ms |
284076 KB |
Output is correct |
19 |
Correct |
415 ms |
284012 KB |
Output is correct |
20 |
Correct |
413 ms |
284128 KB |
Output is correct |
21 |
Correct |
464 ms |
283616 KB |
Output is correct |
22 |
Correct |
415 ms |
283564 KB |
Output is correct |
23 |
Correct |
436 ms |
283636 KB |
Output is correct |
24 |
Correct |
416 ms |
284096 KB |
Output is correct |
25 |
Correct |
415 ms |
284264 KB |
Output is correct |
26 |
Correct |
438 ms |
283988 KB |
Output is correct |
27 |
Correct |
417 ms |
284120 KB |
Output is correct |
28 |
Correct |
428 ms |
284072 KB |
Output is correct |
29 |
Correct |
422 ms |
284844 KB |
Output is correct |
30 |
Correct |
430 ms |
284840 KB |
Output is correct |
31 |
Correct |
433 ms |
284664 KB |
Output is correct |
32 |
Correct |
14 ms |
37976 KB |
Output is correct |
33 |
Correct |
19 ms |
37980 KB |
Output is correct |
34 |
Correct |
16 ms |
37980 KB |
Output is correct |
35 |
Correct |
14 ms |
37980 KB |
Output is correct |
36 |
Correct |
15 ms |
37980 KB |
Output is correct |
37 |
Correct |
15 ms |
37980 KB |
Output is correct |
38 |
Correct |
1541 ms |
286080 KB |
Output is correct |
39 |
Correct |
1540 ms |
286124 KB |
Output is correct |
40 |
Correct |
1504 ms |
286168 KB |
Output is correct |
41 |
Correct |
1505 ms |
285988 KB |
Output is correct |
42 |
Correct |
1555 ms |
286136 KB |
Output is correct |
43 |
Correct |
1476 ms |
286124 KB |
Output is correct |
44 |
Correct |
241 ms |
41808 KB |
Output is correct |
45 |
Correct |
197 ms |
38992 KB |
Output is correct |
46 |
Correct |
1040 ms |
287280 KB |
Output is correct |
47 |
Correct |
1034 ms |
287240 KB |
Output is correct |
48 |
Correct |
1054 ms |
287204 KB |
Output is correct |
49 |
Correct |
216 ms |
41812 KB |
Output is correct |
50 |
Correct |
1630 ms |
288192 KB |
Output is correct |
51 |
Correct |
1692 ms |
288244 KB |
Output is correct |
52 |
Correct |
1632 ms |
287560 KB |
Output is correct |
53 |
Correct |
1575 ms |
288252 KB |
Output is correct |
54 |
Correct |
1596 ms |
288168 KB |
Output is correct |
55 |
Correct |
1626 ms |
288172 KB |
Output is correct |
56 |
Correct |
692 ms |
288940 KB |
Output is correct |
57 |
Correct |
715 ms |
288888 KB |
Output is correct |
58 |
Correct |
694 ms |
288936 KB |
Output is correct |