#include <bits/stdc++.h>
using namespace std;
long long a[200005], ans[200005];
vector<long long> adj[200005];
stack<long long> st;
void dfs(long long x)
{
for (auto y: adj[x])
{
ans[y]=ans[x]+abs(a[x]-a[y]);
dfs(y);
};
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long n, q, i, j, k;
cin >> n;
for (i=1; i<=n; i++)
{
cin >> a[i];
};
for (i=2; i<n; i++)
{
if (a[i]-a[i-1]<=a[i+1]-a[i])
{
while (!st.empty() && a[st.top()]-a[st.top()-1]<=a[i+1]-a[st.top()]) {st.pop();};
if (st.empty()) {adj[1].push_back(i);}
else {adj[st.top()].push_back(i);};
}
else {st.push(i);};
};
while (!st.empty()) {st.pop();};
for (i=n-1; i>1; i--)
{
if (a[i]-a[i-1]>a[i+1]-a[i])
{
while (!st.empty() && a[st.top()+1]-a[st.top()]<a[st.top()]-a[i-1]) {st.pop();};
if (st.empty()) {adj[n].push_back(i);}
else {adj[st.top()].push_back(i);};
}
else {st.push(i);};
};
ans[1]=ans[n]=a[n]-a[1];
dfs(1);
dfs(n);
cin >> q;
while (q--)
{
cin >> i;
j=upper_bound(a+1, a+n+1, i)-a-1;
k=j+1;
if (j==0) {cout << abs(i-a[k])+ans[k] << "\n";}
else if (k==n+1) {cout << abs(i-a[j])+ans[j] << "\n";}
else if (i-a[j]<=a[k]-i) {cout << abs(i-a[j])+ans[j] << "\n";}
else {cout << abs(i-a[k])+ans[k] << "\n";};
};
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6904 KB |
Output is correct |
4 |
Correct |
2 ms |
6740 KB |
Output is correct |
5 |
Correct |
2 ms |
6820 KB |
Output is correct |
6 |
Correct |
2 ms |
6740 KB |
Output is correct |
7 |
Correct |
2 ms |
6740 KB |
Output is correct |
8 |
Correct |
1 ms |
6740 KB |
Output is correct |
9 |
Correct |
2 ms |
6740 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
1 ms |
6740 KB |
Output is correct |
12 |
Correct |
2 ms |
6740 KB |
Output is correct |
13 |
Correct |
1 ms |
6740 KB |
Output is correct |
14 |
Correct |
2 ms |
6900 KB |
Output is correct |
15 |
Correct |
2 ms |
6740 KB |
Output is correct |
16 |
Correct |
2 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6904 KB |
Output is correct |
4 |
Correct |
2 ms |
6740 KB |
Output is correct |
5 |
Correct |
2 ms |
6820 KB |
Output is correct |
6 |
Correct |
2 ms |
6740 KB |
Output is correct |
7 |
Correct |
2 ms |
6740 KB |
Output is correct |
8 |
Correct |
1 ms |
6740 KB |
Output is correct |
9 |
Correct |
2 ms |
6740 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
1 ms |
6740 KB |
Output is correct |
12 |
Correct |
2 ms |
6740 KB |
Output is correct |
13 |
Correct |
1 ms |
6740 KB |
Output is correct |
14 |
Correct |
2 ms |
6900 KB |
Output is correct |
15 |
Correct |
2 ms |
6740 KB |
Output is correct |
16 |
Correct |
2 ms |
6740 KB |
Output is correct |
17 |
Correct |
21 ms |
12364 KB |
Output is correct |
18 |
Correct |
22 ms |
12372 KB |
Output is correct |
19 |
Correct |
22 ms |
13132 KB |
Output is correct |
20 |
Correct |
23 ms |
13140 KB |
Output is correct |
21 |
Correct |
23 ms |
12628 KB |
Output is correct |
22 |
Correct |
23 ms |
12620 KB |
Output is correct |
23 |
Correct |
23 ms |
12620 KB |
Output is correct |
24 |
Correct |
26 ms |
13260 KB |
Output is correct |
25 |
Correct |
25 ms |
13004 KB |
Output is correct |
26 |
Correct |
24 ms |
13004 KB |
Output is correct |
27 |
Correct |
24 ms |
13004 KB |
Output is correct |
28 |
Correct |
25 ms |
13024 KB |
Output is correct |
29 |
Correct |
23 ms |
12884 KB |
Output is correct |
30 |
Correct |
25 ms |
12876 KB |
Output is correct |
31 |
Correct |
27 ms |
12884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
1 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
62 ms |
15908 KB |
Output is correct |
8 |
Correct |
63 ms |
15824 KB |
Output is correct |
9 |
Correct |
68 ms |
15900 KB |
Output is correct |
10 |
Correct |
64 ms |
15824 KB |
Output is correct |
11 |
Correct |
66 ms |
15812 KB |
Output is correct |
12 |
Correct |
63 ms |
15880 KB |
Output is correct |
13 |
Correct |
30 ms |
10572 KB |
Output is correct |
14 |
Correct |
21 ms |
8020 KB |
Output is correct |
15 |
Correct |
71 ms |
15924 KB |
Output is correct |
16 |
Correct |
73 ms |
16028 KB |
Output is correct |
17 |
Correct |
65 ms |
16116 KB |
Output is correct |
18 |
Correct |
29 ms |
10464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6904 KB |
Output is correct |
4 |
Correct |
2 ms |
6740 KB |
Output is correct |
5 |
Correct |
2 ms |
6820 KB |
Output is correct |
6 |
Correct |
2 ms |
6740 KB |
Output is correct |
7 |
Correct |
2 ms |
6740 KB |
Output is correct |
8 |
Correct |
1 ms |
6740 KB |
Output is correct |
9 |
Correct |
2 ms |
6740 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
1 ms |
6740 KB |
Output is correct |
12 |
Correct |
2 ms |
6740 KB |
Output is correct |
13 |
Correct |
1 ms |
6740 KB |
Output is correct |
14 |
Correct |
2 ms |
6900 KB |
Output is correct |
15 |
Correct |
2 ms |
6740 KB |
Output is correct |
16 |
Correct |
2 ms |
6740 KB |
Output is correct |
17 |
Correct |
21 ms |
12364 KB |
Output is correct |
18 |
Correct |
22 ms |
12372 KB |
Output is correct |
19 |
Correct |
22 ms |
13132 KB |
Output is correct |
20 |
Correct |
23 ms |
13140 KB |
Output is correct |
21 |
Correct |
23 ms |
12628 KB |
Output is correct |
22 |
Correct |
23 ms |
12620 KB |
Output is correct |
23 |
Correct |
23 ms |
12620 KB |
Output is correct |
24 |
Correct |
26 ms |
13260 KB |
Output is correct |
25 |
Correct |
25 ms |
13004 KB |
Output is correct |
26 |
Correct |
24 ms |
13004 KB |
Output is correct |
27 |
Correct |
24 ms |
13004 KB |
Output is correct |
28 |
Correct |
25 ms |
13024 KB |
Output is correct |
29 |
Correct |
23 ms |
12884 KB |
Output is correct |
30 |
Correct |
25 ms |
12876 KB |
Output is correct |
31 |
Correct |
27 ms |
12884 KB |
Output is correct |
32 |
Correct |
2 ms |
6736 KB |
Output is correct |
33 |
Correct |
2 ms |
6736 KB |
Output is correct |
34 |
Correct |
2 ms |
6736 KB |
Output is correct |
35 |
Correct |
2 ms |
6736 KB |
Output is correct |
36 |
Correct |
1 ms |
6736 KB |
Output is correct |
37 |
Correct |
2 ms |
6736 KB |
Output is correct |
38 |
Correct |
62 ms |
15908 KB |
Output is correct |
39 |
Correct |
63 ms |
15824 KB |
Output is correct |
40 |
Correct |
68 ms |
15900 KB |
Output is correct |
41 |
Correct |
64 ms |
15824 KB |
Output is correct |
42 |
Correct |
66 ms |
15812 KB |
Output is correct |
43 |
Correct |
63 ms |
15880 KB |
Output is correct |
44 |
Correct |
30 ms |
10572 KB |
Output is correct |
45 |
Correct |
21 ms |
8020 KB |
Output is correct |
46 |
Correct |
71 ms |
15924 KB |
Output is correct |
47 |
Correct |
73 ms |
16028 KB |
Output is correct |
48 |
Correct |
65 ms |
16116 KB |
Output is correct |
49 |
Correct |
29 ms |
10464 KB |
Output is correct |
50 |
Correct |
79 ms |
16460 KB |
Output is correct |
51 |
Correct |
91 ms |
17484 KB |
Output is correct |
52 |
Correct |
83 ms |
16716 KB |
Output is correct |
53 |
Correct |
82 ms |
17328 KB |
Output is correct |
54 |
Correct |
88 ms |
17348 KB |
Output is correct |
55 |
Correct |
83 ms |
17348 KB |
Output is correct |
56 |
Correct |
56 ms |
16952 KB |
Output is correct |
57 |
Correct |
55 ms |
16972 KB |
Output is correct |
58 |
Correct |
58 ms |
16976 KB |
Output is correct |