#include <bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const ll maxn = 1e6 + 2, LG = 21, mod = 1e9 + 7, inf = 1e18;
ll n, a[maxn], L[maxn], R[maxn], d[maxn], st[LG][maxn], low, high, mid, q, s;
vector<int> adj[maxn];
ll getL (int l, int r)
{
int k = __lg(r - l + 1);
return max(st[k][l], st[k][r - (1 << k) + 1]);
}
ll getR (int l, int r)
{
int k = __lg(r - l + 1);
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
void dfs (int u, int p)
{
for (int v : adj[u])
{
if (v == p) continue;
d[v] = d[u] + abs(a[u] - a[v]);
dfs(v, u);
}
}
int main()
{
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++)
st[0][i] = (i == 1) ? inf : 2*a[i] - a[i - 1];
for (int j = 1; j < LG; j++)
for (int i = 1; i <= n - (1 << (j - 1)); i++)
st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
for (int i = 2; i < n; i++)
{
low = 1; high = i;
while (low <= high)
{
mid = (low + high)/2;
if (getL(mid, i) > a[i + 1]) low = mid + 1;
else high = mid - 1;
}
L[i] = high;
}
for (int i = 1; i <= n; i++)
st[0][i] = (i == n) ? -inf : 2*a[i] - a[i + 1];
for (int j = 1; j < LG; j++)
for (int i = 1; i <= n - (1 << (j - 1)); i++)
st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
for (int i = 2; i < n; i++)
{
low = i; high = n;
while (low <= high)
{
mid = (low + high)/2;
if (getR(i, mid) <= a[i - 1]) high = mid - 1;
else low = mid + 1;
}
R[i] = low;
}
for (int i = 2; i < n; i++)
{
adj[i].push_back(L[i] ^ R[i] ^ i);
adj[L[i] ^ R[i] ^ i].push_back(i);
}
a[0] = -inf;
a[n + 1] = inf;
d[1] = d[n] = a[n] - a[1];
dfs(1, 0);
dfs(n, 0);
cin >> q;
while (q--)
{
cin >> s;
int r = upper_bound(a + 1, a + n + 1, s) - a, l = r - 1;
if (s - a[l] <= a[r] - s) cout << abs(s - a[l]) + d[l] << '\n';
else cout << abs(s - a[r]) + d[r] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
39504 KB |
Output is correct |
2 |
Correct |
10 ms |
56144 KB |
Output is correct |
3 |
Correct |
9 ms |
56144 KB |
Output is correct |
4 |
Correct |
9 ms |
56144 KB |
Output is correct |
5 |
Correct |
9 ms |
56060 KB |
Output is correct |
6 |
Correct |
9 ms |
56320 KB |
Output is correct |
7 |
Correct |
9 ms |
56144 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
6 ms |
29320 KB |
Output is correct |
10 |
Correct |
7 ms |
29292 KB |
Output is correct |
11 |
Correct |
5 ms |
29264 KB |
Output is correct |
12 |
Correct |
6 ms |
31312 KB |
Output is correct |
13 |
Correct |
9 ms |
41720 KB |
Output is correct |
14 |
Correct |
9 ms |
56152 KB |
Output is correct |
15 |
Correct |
10 ms |
56152 KB |
Output is correct |
16 |
Correct |
10 ms |
56312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
39504 KB |
Output is correct |
2 |
Correct |
10 ms |
56144 KB |
Output is correct |
3 |
Correct |
9 ms |
56144 KB |
Output is correct |
4 |
Correct |
9 ms |
56144 KB |
Output is correct |
5 |
Correct |
9 ms |
56060 KB |
Output is correct |
6 |
Correct |
9 ms |
56320 KB |
Output is correct |
7 |
Correct |
9 ms |
56144 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
6 ms |
29320 KB |
Output is correct |
10 |
Correct |
7 ms |
29292 KB |
Output is correct |
11 |
Correct |
5 ms |
29264 KB |
Output is correct |
12 |
Correct |
6 ms |
31312 KB |
Output is correct |
13 |
Correct |
9 ms |
41720 KB |
Output is correct |
14 |
Correct |
9 ms |
56152 KB |
Output is correct |
15 |
Correct |
10 ms |
56152 KB |
Output is correct |
16 |
Correct |
10 ms |
56312 KB |
Output is correct |
17 |
Correct |
76 ms |
113488 KB |
Output is correct |
18 |
Correct |
75 ms |
113484 KB |
Output is correct |
19 |
Correct |
77 ms |
114492 KB |
Output is correct |
20 |
Correct |
82 ms |
114460 KB |
Output is correct |
21 |
Correct |
86 ms |
112308 KB |
Output is correct |
22 |
Correct |
77 ms |
113736 KB |
Output is correct |
23 |
Correct |
83 ms |
114248 KB |
Output is correct |
24 |
Correct |
73 ms |
113728 KB |
Output is correct |
25 |
Correct |
74 ms |
113700 KB |
Output is correct |
26 |
Correct |
87 ms |
113216 KB |
Output is correct |
27 |
Correct |
76 ms |
113152 KB |
Output is correct |
28 |
Correct |
76 ms |
113728 KB |
Output is correct |
29 |
Correct |
78 ms |
113992 KB |
Output is correct |
30 |
Correct |
77 ms |
114088 KB |
Output is correct |
31 |
Correct |
77 ms |
113736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
39504 KB |
Output is correct |
2 |
Correct |
7 ms |
41552 KB |
Output is correct |
3 |
Correct |
6 ms |
29264 KB |
Output is correct |
4 |
Correct |
6 ms |
29264 KB |
Output is correct |
5 |
Correct |
7 ms |
29264 KB |
Output is correct |
6 |
Correct |
5 ms |
29264 KB |
Output is correct |
7 |
Correct |
104 ms |
116208 KB |
Output is correct |
8 |
Correct |
106 ms |
116248 KB |
Output is correct |
9 |
Correct |
106 ms |
115900 KB |
Output is correct |
10 |
Correct |
98 ms |
116156 KB |
Output is correct |
11 |
Correct |
99 ms |
115900 KB |
Output is correct |
12 |
Correct |
98 ms |
115900 KB |
Output is correct |
13 |
Correct |
31 ms |
32980 KB |
Output is correct |
14 |
Correct |
26 ms |
30524 KB |
Output is correct |
15 |
Correct |
101 ms |
116792 KB |
Output is correct |
16 |
Correct |
113 ms |
116792 KB |
Output is correct |
17 |
Correct |
102 ms |
115656 KB |
Output is correct |
18 |
Correct |
32 ms |
32840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
39504 KB |
Output is correct |
2 |
Correct |
10 ms |
56144 KB |
Output is correct |
3 |
Correct |
9 ms |
56144 KB |
Output is correct |
4 |
Correct |
9 ms |
56144 KB |
Output is correct |
5 |
Correct |
9 ms |
56060 KB |
Output is correct |
6 |
Correct |
9 ms |
56320 KB |
Output is correct |
7 |
Correct |
9 ms |
56144 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
6 ms |
29320 KB |
Output is correct |
10 |
Correct |
7 ms |
29292 KB |
Output is correct |
11 |
Correct |
5 ms |
29264 KB |
Output is correct |
12 |
Correct |
6 ms |
31312 KB |
Output is correct |
13 |
Correct |
9 ms |
41720 KB |
Output is correct |
14 |
Correct |
9 ms |
56152 KB |
Output is correct |
15 |
Correct |
10 ms |
56152 KB |
Output is correct |
16 |
Correct |
10 ms |
56312 KB |
Output is correct |
17 |
Correct |
76 ms |
113488 KB |
Output is correct |
18 |
Correct |
75 ms |
113484 KB |
Output is correct |
19 |
Correct |
77 ms |
114492 KB |
Output is correct |
20 |
Correct |
82 ms |
114460 KB |
Output is correct |
21 |
Correct |
86 ms |
112308 KB |
Output is correct |
22 |
Correct |
77 ms |
113736 KB |
Output is correct |
23 |
Correct |
83 ms |
114248 KB |
Output is correct |
24 |
Correct |
73 ms |
113728 KB |
Output is correct |
25 |
Correct |
74 ms |
113700 KB |
Output is correct |
26 |
Correct |
87 ms |
113216 KB |
Output is correct |
27 |
Correct |
76 ms |
113152 KB |
Output is correct |
28 |
Correct |
76 ms |
113728 KB |
Output is correct |
29 |
Correct |
78 ms |
113992 KB |
Output is correct |
30 |
Correct |
77 ms |
114088 KB |
Output is correct |
31 |
Correct |
77 ms |
113736 KB |
Output is correct |
32 |
Correct |
7 ms |
39504 KB |
Output is correct |
33 |
Correct |
7 ms |
41552 KB |
Output is correct |
34 |
Correct |
6 ms |
29264 KB |
Output is correct |
35 |
Correct |
6 ms |
29264 KB |
Output is correct |
36 |
Correct |
7 ms |
29264 KB |
Output is correct |
37 |
Correct |
5 ms |
29264 KB |
Output is correct |
38 |
Correct |
104 ms |
116208 KB |
Output is correct |
39 |
Correct |
106 ms |
116248 KB |
Output is correct |
40 |
Correct |
106 ms |
115900 KB |
Output is correct |
41 |
Correct |
98 ms |
116156 KB |
Output is correct |
42 |
Correct |
99 ms |
115900 KB |
Output is correct |
43 |
Correct |
98 ms |
115900 KB |
Output is correct |
44 |
Correct |
31 ms |
32980 KB |
Output is correct |
45 |
Correct |
26 ms |
30524 KB |
Output is correct |
46 |
Correct |
101 ms |
116792 KB |
Output is correct |
47 |
Correct |
113 ms |
116792 KB |
Output is correct |
48 |
Correct |
102 ms |
115656 KB |
Output is correct |
49 |
Correct |
32 ms |
32840 KB |
Output is correct |
50 |
Correct |
114 ms |
117624 KB |
Output is correct |
51 |
Correct |
119 ms |
118088 KB |
Output is correct |
52 |
Correct |
137 ms |
117864 KB |
Output is correct |
53 |
Correct |
137 ms |
117316 KB |
Output is correct |
54 |
Correct |
143 ms |
118080 KB |
Output is correct |
55 |
Correct |
138 ms |
117824 KB |
Output is correct |
56 |
Correct |
112 ms |
117320 KB |
Output is correct |
57 |
Correct |
114 ms |
118048 KB |
Output is correct |
58 |
Correct |
112 ms |
117320 KB |
Output is correct |