#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
const int N=2e5+10, inf=1e18;
struct Node{
int val1, val2;
Node (int t1=0, int t2=0){
val1=t1;
val2=t2;
}
Node operator+(Node &b){
return Node(max(val1, b.val1), min(val2, b.val2));
}
};
struct SegmentTree{
int n;
vector<Node> t;
void init(int _n){
n=_n;
t.assign(4*n+1, Node());
}
void build(int k, int l, int r, int *a){
if (l==r){
t[k]=Node(a[l]*2-a[l-1], a[l]*2-a[l+1]);
return;
}
int mid=(l+r)>>1;
build(k<<1, l, mid, a);
build(k<<1|1, mid+1, r, a);
t[k]=t[k<<1]+t[k<<1|1];
}
int walk1(int k, int l, int r, int pos, int val){
if (t[k].val1<=val || l>pos) return 1;
if (l==r) return l;
int mid=(l+r)>>1;
if (t[k<<1|1].val1<=val) return walk1(k<<1, l, mid, pos, val);
int tmp=walk1(k<<1|1, mid+1, r, pos, val);
if (tmp==1) return walk1(k<<1, l, mid, pos, val);
return tmp;
}
int walk2(int k, int l, int r, int pos, int val){
if (t[k].val2>val || r<pos) return n;
if (l==r) return l;
int mid=(l+r)>>1;
if (t[k<<1].val2>val) return walk2(k<<1|1, mid+1, r, pos, val);
int tmp=walk2(k<<1, l, mid, pos, val);
if (tmp==n) return walk2(k<<1|1, mid+1, r, pos, val);
return tmp;
}
} st;
int n, q, a[N];
void solve(){
cin >> n;
for (int i=1; i<=n; ++i) cin >> a[i];
cin >> q;
a[0]=-inf, a[n+1]=inf;
st.init(n);
st.build(1, 1, n, a);
while (q--){
int s; cin >> s;
int ir=lower_bound(a+1, a+n+1, s)-a;
int il=ir-1;
int cur=s, ans=0;
while (il>=1 || ir<=n){
if (il>=1 && (ir>n || abs(cur-a[il])<=abs(cur-a[ir]))){
int i=st.walk1(1, 1, n, il, a[ir]);
ans+=abs(cur-a[i]);
il=i-1;
cur=a[i];
// dist(i, i-1) > dist(i, ir)
// a[i] - a[i-1] > a[ir] - a[i]
// 2 a[i] - a[i-1] > a[ir]
// tim i dau tien <= il
}else{
int i=st.walk2(1, 1, n, ir, a[il]);
ans+=abs(cur-a[i]);
ir=i+1;
cur=a[i];
// dist(il, i) <= dist(i, i+1)
// a[i] - a[il] <= a[i+1] - a[i]
// 2a[i] - a[i+1] <= a[il]
// tim i dau tien >= ir
}
}
cout << ans << '\n';
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
23 ms |
16444 KB |
Output is correct |
18 |
Correct |
20 ms |
16464 KB |
Output is correct |
19 |
Correct |
24 ms |
16656 KB |
Output is correct |
20 |
Correct |
20 ms |
16476 KB |
Output is correct |
21 |
Correct |
22 ms |
16468 KB |
Output is correct |
22 |
Correct |
20 ms |
16404 KB |
Output is correct |
23 |
Correct |
23 ms |
16348 KB |
Output is correct |
24 |
Correct |
20 ms |
16476 KB |
Output is correct |
25 |
Correct |
19 ms |
16476 KB |
Output is correct |
26 |
Correct |
20 ms |
16476 KB |
Output is correct |
27 |
Correct |
20 ms |
16468 KB |
Output is correct |
28 |
Correct |
19 ms |
16468 KB |
Output is correct |
29 |
Correct |
20 ms |
16392 KB |
Output is correct |
30 |
Correct |
20 ms |
16220 KB |
Output is correct |
31 |
Correct |
20 ms |
16316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
137 ms |
18504 KB |
Output is correct |
8 |
Correct |
144 ms |
18464 KB |
Output is correct |
9 |
Correct |
118 ms |
18264 KB |
Output is correct |
10 |
Correct |
114 ms |
18444 KB |
Output is correct |
11 |
Correct |
117 ms |
18272 KB |
Output is correct |
12 |
Correct |
150 ms |
18288 KB |
Output is correct |
13 |
Correct |
34 ms |
4176 KB |
Output is correct |
14 |
Correct |
32 ms |
1516 KB |
Output is correct |
15 |
Correct |
94 ms |
19540 KB |
Output is correct |
16 |
Correct |
97 ms |
19540 KB |
Output is correct |
17 |
Correct |
97 ms |
19536 KB |
Output is correct |
18 |
Correct |
43 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
23 ms |
16444 KB |
Output is correct |
18 |
Correct |
20 ms |
16464 KB |
Output is correct |
19 |
Correct |
24 ms |
16656 KB |
Output is correct |
20 |
Correct |
20 ms |
16476 KB |
Output is correct |
21 |
Correct |
22 ms |
16468 KB |
Output is correct |
22 |
Correct |
20 ms |
16404 KB |
Output is correct |
23 |
Correct |
23 ms |
16348 KB |
Output is correct |
24 |
Correct |
20 ms |
16476 KB |
Output is correct |
25 |
Correct |
19 ms |
16476 KB |
Output is correct |
26 |
Correct |
20 ms |
16476 KB |
Output is correct |
27 |
Correct |
20 ms |
16468 KB |
Output is correct |
28 |
Correct |
19 ms |
16468 KB |
Output is correct |
29 |
Correct |
20 ms |
16392 KB |
Output is correct |
30 |
Correct |
20 ms |
16220 KB |
Output is correct |
31 |
Correct |
20 ms |
16316 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
137 ms |
18504 KB |
Output is correct |
39 |
Correct |
144 ms |
18464 KB |
Output is correct |
40 |
Correct |
118 ms |
18264 KB |
Output is correct |
41 |
Correct |
114 ms |
18444 KB |
Output is correct |
42 |
Correct |
117 ms |
18272 KB |
Output is correct |
43 |
Correct |
150 ms |
18288 KB |
Output is correct |
44 |
Correct |
34 ms |
4176 KB |
Output is correct |
45 |
Correct |
32 ms |
1516 KB |
Output is correct |
46 |
Correct |
94 ms |
19540 KB |
Output is correct |
47 |
Correct |
97 ms |
19540 KB |
Output is correct |
48 |
Correct |
97 ms |
19536 KB |
Output is correct |
49 |
Correct |
43 ms |
4188 KB |
Output is correct |
50 |
Correct |
162 ms |
20416 KB |
Output is correct |
51 |
Correct |
169 ms |
20460 KB |
Output is correct |
52 |
Correct |
155 ms |
20404 KB |
Output is correct |
53 |
Correct |
149 ms |
20580 KB |
Output is correct |
54 |
Correct |
129 ms |
20372 KB |
Output is correct |
55 |
Correct |
150 ms |
20580 KB |
Output is correct |
56 |
Correct |
88 ms |
20304 KB |
Output is correct |
57 |
Correct |
72 ms |
20304 KB |
Output is correct |
58 |
Correct |
75 ms |
20744 KB |
Output is correct |