#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int maxn = 2e5+10;
const int inf = 2e9+10;
int n, a[maxn], tr1[4*maxn], tr2[4*maxn];
void build1(int no, int l, int r) {
if(l == r) {
if(l == n) tr1[no] = -inf;
else tr1[no] = 2*a[l]-a[l+1];
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
build1(lc,l,mid);
build1(rc,mid+1,r);
tr1[no] = min(tr1[lc],tr1[rc]);
}
int cnt = 0;
// leftmost i with i >= pos and x[i] <= val
int find1(int no, int l, int r, int pos, int val) {assert(++cnt <= 70);
if(tr1[no] > val || r < pos) return -1;
if(l == r) {
return l;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
if(l >= pos) {
if(tr1[lc] <= val) return find1(lc,l,mid,pos,val);
return find1(rc,mid+1,r,pos,val);
}
int ansl = find1(lc,l,mid,pos,val);
if(ansl != -1) return ansl;
return find1(rc,mid+1,r,pos,val);
}
void build2(int no, int l, int r) {
if(l == r) {
if(l == 1) tr2[no] = inf;
else tr2[no] = 2*a[l]-a[l-1];
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
build2(lc,l,mid);
build2(rc,mid+1,r);
tr2[no] = max(tr2[lc],tr2[rc]);
}
// rightmost i with i <= pos and x[i] >= val
int find2(int no, int l, int r, int pos, int val) {assert(++cnt <= 70);
if(tr2[no] < val || l > pos) return -1;
if(l == r) {
return l;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
if(r <= pos) {
if(tr2[rc] >= val) return find2(rc,mid+1,r,pos,val);
return find2(lc,l,mid,pos,val);
}
int ansr = find2(rc,mid+1,r,pos,val);
if(ansr != -1) return ansr;
return find2(lc,l,mid,pos,val);
}
int32_t main() {
// freopen("in.in","r",stdin);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
build1(1,1,n);
build2(1,1,n);
int q; cin >> q;
// assert(q == 1);
while(q--) {
int l = -1,r, sd = 0;
int p; cin >> p;
long long ans = 0;
int l1 = 0;
int r1 = n;
int ansbs = -1;
while(l1 <= r1) {
int mid = (l1+r1)/2;
if(mid == n || a[mid+1] >= p) {
ansbs = mid;
r1 = mid-1;
}
else {
l1 = mid+1;
}
}
if(ansbs != 0 && (ansbs == n || p-a[ansbs] <= a[ansbs+1] - p)) {
ans+= p-a[ansbs];
l = ansbs;
r = ansbs;
}
else {
ans+= a[ansbs+1]-p;
l = ansbs+1;
r = ansbs+1;
}
int qtalt = 0;
while(l != 1 || r != n) {
if(sd == 0) {
if(l == 1) {
ans+= a[n]-a[l];
r = n;
sd = 1;
qtalt++;
}
else if(r == n) {
ans+= a[l]-a[1];
l = 1;
qtalt++;
}
else if(a[l]-a[l-1] <= a[r+1]-a[l]) {
cnt = 0;
int ansb = find2(1,1,n,l-1,a[r+1]+1);
assert(cnt <= 70);
ans+= a[l]-a[ansb];
l = ansb;
qtalt++;
}
else {
cnt = 0;
int ansb = find1(1,1,n,r+1,a[l-1]);
assert(cnt <= 70);
ans+= a[ansb]-a[l];
r = ansb;
sd = 1;
qtalt++;
}
}
else {
if(l == 1) {
ans+= a[n]-a[r];
r = n;
qtalt++;
}
else if(r == n) {
ans+= a[r]-a[1];
sd = 0;
l = 1;
qtalt++;
}
else if(a[r]-a[l-1] <= a[r+1]-a[r]) {
cnt = 0;
int ansb = find2(1,1,n,l-1,a[r+1]+1);
assert(cnt <= 70);
ans+= a[r]-a[ansb];
sd = 0;
l = ansb;
qtalt++;
}
else {
cnt = 0;
int ansb = find1(1,1,n,r+1,a[l-1]);
assert(cnt <= 70);
ans+= a[ansb]-a[r];
r = ansb;
qtalt++;
}
}
}
assert(qtalt <= 20);
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4548 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4548 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
59 ms |
9196 KB |
Output is correct |
18 |
Correct |
58 ms |
9376 KB |
Output is correct |
19 |
Correct |
58 ms |
9300 KB |
Output is correct |
20 |
Correct |
59 ms |
9552 KB |
Output is correct |
21 |
Correct |
59 ms |
9300 KB |
Output is correct |
22 |
Correct |
58 ms |
9300 KB |
Output is correct |
23 |
Correct |
71 ms |
9300 KB |
Output is correct |
24 |
Correct |
72 ms |
9300 KB |
Output is correct |
25 |
Correct |
68 ms |
9480 KB |
Output is correct |
26 |
Correct |
59 ms |
9264 KB |
Output is correct |
27 |
Correct |
58 ms |
9300 KB |
Output is correct |
28 |
Correct |
59 ms |
9380 KB |
Output is correct |
29 |
Correct |
57 ms |
9296 KB |
Output is correct |
30 |
Correct |
60 ms |
9188 KB |
Output is correct |
31 |
Correct |
57 ms |
9300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
383 ms |
11192 KB |
Output is correct |
8 |
Correct |
389 ms |
11284 KB |
Output is correct |
9 |
Correct |
380 ms |
11184 KB |
Output is correct |
10 |
Correct |
376 ms |
11164 KB |
Output is correct |
11 |
Correct |
378 ms |
11344 KB |
Output is correct |
12 |
Correct |
384 ms |
11344 KB |
Output is correct |
13 |
Correct |
296 ms |
8228 KB |
Output is correct |
14 |
Correct |
264 ms |
5588 KB |
Output is correct |
15 |
Correct |
375 ms |
12372 KB |
Output is correct |
16 |
Correct |
389 ms |
12240 KB |
Output is correct |
17 |
Correct |
378 ms |
12348 KB |
Output is correct |
18 |
Correct |
307 ms |
8180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4548 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4440 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
59 ms |
9196 KB |
Output is correct |
18 |
Correct |
58 ms |
9376 KB |
Output is correct |
19 |
Correct |
58 ms |
9300 KB |
Output is correct |
20 |
Correct |
59 ms |
9552 KB |
Output is correct |
21 |
Correct |
59 ms |
9300 KB |
Output is correct |
22 |
Correct |
58 ms |
9300 KB |
Output is correct |
23 |
Correct |
71 ms |
9300 KB |
Output is correct |
24 |
Correct |
72 ms |
9300 KB |
Output is correct |
25 |
Correct |
68 ms |
9480 KB |
Output is correct |
26 |
Correct |
59 ms |
9264 KB |
Output is correct |
27 |
Correct |
58 ms |
9300 KB |
Output is correct |
28 |
Correct |
59 ms |
9380 KB |
Output is correct |
29 |
Correct |
57 ms |
9296 KB |
Output is correct |
30 |
Correct |
60 ms |
9188 KB |
Output is correct |
31 |
Correct |
57 ms |
9300 KB |
Output is correct |
32 |
Correct |
1 ms |
4440 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4440 KB |
Output is correct |
38 |
Correct |
383 ms |
11192 KB |
Output is correct |
39 |
Correct |
389 ms |
11284 KB |
Output is correct |
40 |
Correct |
380 ms |
11184 KB |
Output is correct |
41 |
Correct |
376 ms |
11164 KB |
Output is correct |
42 |
Correct |
378 ms |
11344 KB |
Output is correct |
43 |
Correct |
384 ms |
11344 KB |
Output is correct |
44 |
Correct |
296 ms |
8228 KB |
Output is correct |
45 |
Correct |
264 ms |
5588 KB |
Output is correct |
46 |
Correct |
375 ms |
12372 KB |
Output is correct |
47 |
Correct |
389 ms |
12240 KB |
Output is correct |
48 |
Correct |
378 ms |
12348 KB |
Output is correct |
49 |
Correct |
307 ms |
8180 KB |
Output is correct |
50 |
Correct |
483 ms |
13552 KB |
Output is correct |
51 |
Correct |
452 ms |
13396 KB |
Output is correct |
52 |
Correct |
445 ms |
13588 KB |
Output is correct |
53 |
Correct |
450 ms |
13560 KB |
Output is correct |
54 |
Correct |
431 ms |
13492 KB |
Output is correct |
55 |
Correct |
452 ms |
13980 KB |
Output is correct |
56 |
Correct |
385 ms |
13652 KB |
Output is correct |
57 |
Correct |
373 ms |
13376 KB |
Output is correct |
58 |
Correct |
381 ms |
13392 KB |
Output is correct |