#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e6 + 6;
const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX/4;
#define bit(x,y) ((x >> y) & 1)
vector<ll> g[N];
ll a[N];
ll d[N];
int n;
void build(ll x, ll dis)
{
d[x] = dis + a[n] - a[1];
for(auto y : g[x])
{
build(y, dis + abs(a[x] - a[y]));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int i,j;
for(i = 1;i <= n;i++)
{
cin >> a[i];
}
a[0] = -inf;
a[n + 1] = inf;
stack<pair<ll,ll>> st;
st.push({1,inf});
for(i = 2;i <= n - 1;i++)
{
if(a[i] - a[i - 1] <= a[i + 1] - a[i])
{
while(st.top().second <= a[i + 1]) st.pop();
j = st.top().first;
g[j].push_back(i);
}
while(st.top().second <= 2 * a[i] - a[i - 1]) st.pop();
st.push({i,2 * a[i] - a[i - 1]});
}
while(!st.empty()) st.pop();
st.push({n,inf});
for(i = n - 1;i >= 2;i--)
{
if(a[i] - a[i - 1] > a[i + 1] - a[i])
{
while(st.top().second < -a[i - 1]) st.pop();
j = st.top().first;
g[j].push_back(i);
}
while(st.top().second <= a[i + 1] - 2 * a[i]) st.pop();
st.push({i,a[i + 1] - 2 * a[i]});
}
build(1,0);
build(n,0);
//for(i = 1;i <= n;i++) cout << d[i] << " ";
ll q;
cin >> q;
while(q--)
{
ll x;
cin >> x;
if(x < a[1]) i = 1;
if(x > a[n]) i = n;
ll l = 1, r = n;
while(true)
{
if(l == r)
{
i = l;
break;
}
ll mid = (l + r + 1) / 2;
if(a[mid] <= x) l = mid;
else r = mid - 1;
}
if(x - a[l] <= a[l + 1] - x) i = l;
else i = l + 1;
cout << abs(x - a[i]) + d[i] << "\n";
}
}//sus
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
27164 KB |
Output is correct |
7 |
Correct |
6 ms |
26960 KB |
Output is correct |
8 |
Correct |
7 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
27096 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26960 KB |
Output is correct |
13 |
Correct |
5 ms |
26960 KB |
Output is correct |
14 |
Correct |
6 ms |
27264 KB |
Output is correct |
15 |
Correct |
8 ms |
27132 KB |
Output is correct |
16 |
Correct |
6 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
27164 KB |
Output is correct |
7 |
Correct |
6 ms |
26960 KB |
Output is correct |
8 |
Correct |
7 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
27096 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26960 KB |
Output is correct |
13 |
Correct |
5 ms |
26960 KB |
Output is correct |
14 |
Correct |
6 ms |
27264 KB |
Output is correct |
15 |
Correct |
8 ms |
27132 KB |
Output is correct |
16 |
Correct |
6 ms |
26960 KB |
Output is correct |
17 |
Correct |
28 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33608 KB |
Output is correct |
19 |
Correct |
28 ms |
34376 KB |
Output is correct |
20 |
Correct |
28 ms |
34384 KB |
Output is correct |
21 |
Correct |
28 ms |
33764 KB |
Output is correct |
22 |
Correct |
28 ms |
33616 KB |
Output is correct |
23 |
Correct |
28 ms |
33616 KB |
Output is correct |
24 |
Correct |
30 ms |
34388 KB |
Output is correct |
25 |
Correct |
30 ms |
34240 KB |
Output is correct |
26 |
Correct |
30 ms |
34240 KB |
Output is correct |
27 |
Correct |
29 ms |
34240 KB |
Output is correct |
28 |
Correct |
29 ms |
34232 KB |
Output is correct |
29 |
Correct |
28 ms |
34128 KB |
Output is correct |
30 |
Correct |
28 ms |
33992 KB |
Output is correct |
31 |
Correct |
29 ms |
34132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26884 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
66 ms |
34752 KB |
Output is correct |
8 |
Correct |
72 ms |
34752 KB |
Output is correct |
9 |
Correct |
91 ms |
34780 KB |
Output is correct |
10 |
Correct |
73 ms |
34900 KB |
Output is correct |
11 |
Correct |
80 ms |
34752 KB |
Output is correct |
12 |
Correct |
71 ms |
34752 KB |
Output is correct |
13 |
Correct |
32 ms |
29008 KB |
Output is correct |
14 |
Correct |
24 ms |
27472 KB |
Output is correct |
15 |
Correct |
69 ms |
36028 KB |
Output is correct |
16 |
Correct |
71 ms |
35904 KB |
Output is correct |
17 |
Correct |
75 ms |
35888 KB |
Output is correct |
18 |
Correct |
32 ms |
29008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
27164 KB |
Output is correct |
7 |
Correct |
6 ms |
26960 KB |
Output is correct |
8 |
Correct |
7 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
27096 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
6 ms |
26960 KB |
Output is correct |
13 |
Correct |
5 ms |
26960 KB |
Output is correct |
14 |
Correct |
6 ms |
27264 KB |
Output is correct |
15 |
Correct |
8 ms |
27132 KB |
Output is correct |
16 |
Correct |
6 ms |
26960 KB |
Output is correct |
17 |
Correct |
28 ms |
33556 KB |
Output is correct |
18 |
Correct |
28 ms |
33608 KB |
Output is correct |
19 |
Correct |
28 ms |
34376 KB |
Output is correct |
20 |
Correct |
28 ms |
34384 KB |
Output is correct |
21 |
Correct |
28 ms |
33764 KB |
Output is correct |
22 |
Correct |
28 ms |
33616 KB |
Output is correct |
23 |
Correct |
28 ms |
33616 KB |
Output is correct |
24 |
Correct |
30 ms |
34388 KB |
Output is correct |
25 |
Correct |
30 ms |
34240 KB |
Output is correct |
26 |
Correct |
30 ms |
34240 KB |
Output is correct |
27 |
Correct |
29 ms |
34240 KB |
Output is correct |
28 |
Correct |
29 ms |
34232 KB |
Output is correct |
29 |
Correct |
28 ms |
34128 KB |
Output is correct |
30 |
Correct |
28 ms |
33992 KB |
Output is correct |
31 |
Correct |
29 ms |
34132 KB |
Output is correct |
32 |
Correct |
6 ms |
26960 KB |
Output is correct |
33 |
Correct |
5 ms |
26884 KB |
Output is correct |
34 |
Correct |
5 ms |
26960 KB |
Output is correct |
35 |
Correct |
6 ms |
26960 KB |
Output is correct |
36 |
Correct |
5 ms |
26960 KB |
Output is correct |
37 |
Correct |
5 ms |
26960 KB |
Output is correct |
38 |
Correct |
66 ms |
34752 KB |
Output is correct |
39 |
Correct |
72 ms |
34752 KB |
Output is correct |
40 |
Correct |
91 ms |
34780 KB |
Output is correct |
41 |
Correct |
73 ms |
34900 KB |
Output is correct |
42 |
Correct |
80 ms |
34752 KB |
Output is correct |
43 |
Correct |
71 ms |
34752 KB |
Output is correct |
44 |
Correct |
32 ms |
29008 KB |
Output is correct |
45 |
Correct |
24 ms |
27472 KB |
Output is correct |
46 |
Correct |
69 ms |
36028 KB |
Output is correct |
47 |
Correct |
71 ms |
35904 KB |
Output is correct |
48 |
Correct |
75 ms |
35888 KB |
Output is correct |
49 |
Correct |
32 ms |
29008 KB |
Output is correct |
50 |
Correct |
87 ms |
35748 KB |
Output is correct |
51 |
Correct |
79 ms |
36676 KB |
Output is correct |
52 |
Correct |
80 ms |
35912 KB |
Output is correct |
53 |
Correct |
89 ms |
36444 KB |
Output is correct |
54 |
Correct |
91 ms |
36544 KB |
Output is correct |
55 |
Correct |
89 ms |
36332 KB |
Output is correct |
56 |
Correct |
62 ms |
36168 KB |
Output is correct |
57 |
Correct |
79 ms |
36168 KB |
Output is correct |
58 |
Correct |
64 ms |
36168 KB |
Output is correct |