#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=2e5+5;
int n, v[nx], q, s, l, r, dr[nx], dl[nx];
long long ans;
bool isl;
struct rsegtree
{
int d[4*nx];
void build(int l, int r, int i)
{
if (l==r) return void(d[i]=dr[l]);
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
d[i]=min(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int ql, int qr, int vl)
{
if (qr<l||r<ql) return -1;
if (ql<=l&&r<=qr&&vl<d[i]) return -1;
if (l==r) return l;
int md=(l+r)/2, tmp=query(l, md, 2*i, ql, qr, vl);
if (tmp!=-1) return tmp;
return query(md+1, r, 2*i+1, ql, qr, vl);
}
} rs;
struct lsegtree
{
int d[4*nx];
void build(int l, int r, int i)
{
if (l==r) return void(d[i]=dl[l]);
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
d[i]=max(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int ql, int qr, int vl)
{
if (qr<l||r<ql) return -1;
if (ql<=l&&r<=qr&&vl>=d[i]) return -1;
if (l==r) return l;
int md=(l+r)/2, tmp=query(md+1, r, 2*i+1, ql, qr, vl);
if (tmp!=-1) return tmp;
return query(l, md, 2*i, ql, qr, vl);
}
} ls;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
v[0]=-1e9; v[n+1]=2e9;
for (int i=1; i<=n; i++) cin>>v[i];
for (int i=1; i<=n; i++) dr[i]=2*v[i]-v[i+1];
for (int i=1; i<=n; i++) dl[i]=2*v[i]-v[i-1];
rs.build(1, n, 1);
ls.build(1, n, 1);
cin>>q;
while (q--)
{
cin>>s;
int cl=lower_bound(v+1, v+n+1, s)-v;
if (s-v[cl-1]<=v[cl]-s) l=r=cl-1, ans=s-v[cl-1];
else l=r=cl, ans=v[cl]-s;
isl=1;
while (l!=1&&r!=n)
{
if (isl&&l!=1)
{
int np=ls.query(1, n, 1, 1, l, v[r+1]);
ans+=v[r]-v[np];
l=np;
}
if (!isl&&r!=n)
{
int np=rs.query(1, n, 1, r, n, v[l-1]);
ans+=v[np]-v[l];
r=np;
}
isl=!isl;
}
//cout<<l<<' '<<r<<' '<<ans<<'\n';
if (r==n) ans+=v[r]-v[1];
else ans+=v[n]-v[l];
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
19 ms |
8696 KB |
Output is correct |
18 |
Correct |
19 ms |
8604 KB |
Output is correct |
19 |
Correct |
19 ms |
8620 KB |
Output is correct |
20 |
Correct |
19 ms |
8688 KB |
Output is correct |
21 |
Correct |
19 ms |
8660 KB |
Output is correct |
22 |
Correct |
19 ms |
8588 KB |
Output is correct |
23 |
Correct |
19 ms |
8680 KB |
Output is correct |
24 |
Correct |
19 ms |
8656 KB |
Output is correct |
25 |
Correct |
19 ms |
8728 KB |
Output is correct |
26 |
Correct |
19 ms |
8616 KB |
Output is correct |
27 |
Correct |
19 ms |
8612 KB |
Output is correct |
28 |
Correct |
19 ms |
8708 KB |
Output is correct |
29 |
Correct |
19 ms |
8640 KB |
Output is correct |
30 |
Correct |
18 ms |
8652 KB |
Output is correct |
31 |
Correct |
19 ms |
8584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
102 ms |
10688 KB |
Output is correct |
8 |
Correct |
109 ms |
10808 KB |
Output is correct |
9 |
Correct |
102 ms |
10664 KB |
Output is correct |
10 |
Correct |
102 ms |
10604 KB |
Output is correct |
11 |
Correct |
102 ms |
10688 KB |
Output is correct |
12 |
Correct |
103 ms |
10624 KB |
Output is correct |
13 |
Correct |
31 ms |
4144 KB |
Output is correct |
14 |
Correct |
30 ms |
1540 KB |
Output is correct |
15 |
Correct |
95 ms |
11724 KB |
Output is correct |
16 |
Correct |
95 ms |
11740 KB |
Output is correct |
17 |
Correct |
95 ms |
11716 KB |
Output is correct |
18 |
Correct |
33 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
19 ms |
8696 KB |
Output is correct |
18 |
Correct |
19 ms |
8604 KB |
Output is correct |
19 |
Correct |
19 ms |
8620 KB |
Output is correct |
20 |
Correct |
19 ms |
8688 KB |
Output is correct |
21 |
Correct |
19 ms |
8660 KB |
Output is correct |
22 |
Correct |
19 ms |
8588 KB |
Output is correct |
23 |
Correct |
19 ms |
8680 KB |
Output is correct |
24 |
Correct |
19 ms |
8656 KB |
Output is correct |
25 |
Correct |
19 ms |
8728 KB |
Output is correct |
26 |
Correct |
19 ms |
8616 KB |
Output is correct |
27 |
Correct |
19 ms |
8612 KB |
Output is correct |
28 |
Correct |
19 ms |
8708 KB |
Output is correct |
29 |
Correct |
19 ms |
8640 KB |
Output is correct |
30 |
Correct |
18 ms |
8652 KB |
Output is correct |
31 |
Correct |
19 ms |
8584 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
102 ms |
10688 KB |
Output is correct |
39 |
Correct |
109 ms |
10808 KB |
Output is correct |
40 |
Correct |
102 ms |
10664 KB |
Output is correct |
41 |
Correct |
102 ms |
10604 KB |
Output is correct |
42 |
Correct |
102 ms |
10688 KB |
Output is correct |
43 |
Correct |
103 ms |
10624 KB |
Output is correct |
44 |
Correct |
31 ms |
4144 KB |
Output is correct |
45 |
Correct |
30 ms |
1540 KB |
Output is correct |
46 |
Correct |
95 ms |
11724 KB |
Output is correct |
47 |
Correct |
95 ms |
11740 KB |
Output is correct |
48 |
Correct |
95 ms |
11716 KB |
Output is correct |
49 |
Correct |
33 ms |
4172 KB |
Output is correct |
50 |
Correct |
199 ms |
12804 KB |
Output is correct |
51 |
Correct |
194 ms |
12892 KB |
Output is correct |
52 |
Correct |
174 ms |
12772 KB |
Output is correct |
53 |
Correct |
148 ms |
12748 KB |
Output is correct |
54 |
Correct |
154 ms |
12988 KB |
Output is correct |
55 |
Correct |
148 ms |
12748 KB |
Output is correct |
56 |
Correct |
85 ms |
12696 KB |
Output is correct |
57 |
Correct |
84 ms |
12836 KB |
Output is correct |
58 |
Correct |
85 ms |
12640 KB |
Output is correct |