#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int MaxN = 1e6 + 5;
const int MaxLog = 21;
int n, q;
long long arr[MaxN];
void Inp()
{
cin >> n;
for (int x = 1; x <= n; x++)
{
cin >> arr[x];
}
sort(arr + 1, arr + n + 1);
}
long long Sparse[MaxLog][MaxN];
void Build()
{
for (int x = 1; x <= n; x++)
{
Sparse[0][x] = 2 * arr[x] - arr[x + 1];
}
for (int x = 1; x < MaxLog; x++)
{
for (int y = 1; y <= n; y++)
{
Sparse[x][y] = min(Sparse[x - 1][y], Sparse[x - 1][y + (1 << (x - 1))]);
}
}
}
void Build2()
{
for (int x = 1; x <= n; x++)
{
Sparse[0][x] = 2 * arr[x] - arr[x - 1];
}
for (int x = 1; x < MaxLog; x++)
{
for (int y = 1; y <= n; y++)
{
Sparse[x][y] = max(Sparse[x - 1][y], Sparse[x - 1][y + (1 << (x - 1))]);
}
}
}
long long Get(int l, int r)
{
int p = __lg(r - l + 1);
return min(Sparse[p][l], Sparse[p][r - (1 << p) + 1]);
}
long long Get2(int l, int r)
{
int p = __lg(r - l + 1);
return max(Sparse[p][l], Sparse[p][r - (1 << p) + 1]);
}
vector<int> graph[MaxN];
long long res[MaxN];
void DFS(int u, int p)
{
for (int x : graph[u])
{
if (x != p)
{
res[x] = res[u] + abs(arr[u] - arr[x]);
DFS(x, u);
}
}
}
void Exc()
{
Build();
for (int x = 2; x < n; x++)
{
if (arr[x + 1] - arr[x] < arr[x] - arr[x - 1])
{
int l = x + 1, r = n - 1, mid, res = n;
while (l <= r)
{
mid = (l + r) / 2;
if (Get(x + 1, mid) <= arr[x - 1])
{
res = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
graph[x].push_back(res);
graph[res].push_back(x);
}
}
Build2();
for (int x = 2; x < n; x++)
{
if (arr[x + 1] - arr[x] >= arr[x] - arr[x - 1])
{
int l = 2, r = x - 1, mid, res = 1;
while (l <= r)
{
mid = (l + r) / 2;
if (Get2(mid, x - 1) > arr[x + 1])
{
res = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
graph[x].push_back(res);
graph[res].push_back(x);
}
}
DFS(1, -1);
DFS(n, -1);
map<long long, int> mp;
for (int x = 1; x <= n; x++)
{
mp[arr[x]] = x;
//cout << res[x] << " ";
}
//cout << "\n";
cin >> q;
for (int x = 1; x <= q; x++)
{
long long p;
cin >> p;
auto q = mp.lower_bound(p);
if (q == mp.begin())
{
cout << res[q->second] + (arr[n] - arr[1]) + abs(q->first - p) << "\n";
}
else if (q == mp.end())
{
--q;
cout << res[q->second] + (arr[n] - arr[1]) + abs(q->first - p) << "\n";
}
else
{
auto k = q;
--k;
if (abs(k->first - p) > abs(q->first - p))
{
cout << res[q->second] + (arr[n] - arr[1]) + abs(q->first - p) << "\n";
}
else
{
cout << res[k->second] + (arr[n] - arr[1]) + abs(k->first - p) << "\n";
}
}
}
}
int main()
{
//freopen("C.INP", "r", stdin);
//freopen("C.OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int test = 1;
//cin >> test;
for (int x = 1; x <= test; x++)
{
Inp();
Exc();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
70224 KB |
Output is correct |
2 |
Correct |
13 ms |
70372 KB |
Output is correct |
3 |
Correct |
10 ms |
70224 KB |
Output is correct |
4 |
Correct |
10 ms |
70428 KB |
Output is correct |
5 |
Correct |
10 ms |
70224 KB |
Output is correct |
6 |
Correct |
11 ms |
70224 KB |
Output is correct |
7 |
Correct |
11 ms |
70224 KB |
Output is correct |
8 |
Correct |
9 ms |
70224 KB |
Output is correct |
9 |
Correct |
9 ms |
70192 KB |
Output is correct |
10 |
Correct |
9 ms |
70224 KB |
Output is correct |
11 |
Correct |
9 ms |
70236 KB |
Output is correct |
12 |
Correct |
11 ms |
70224 KB |
Output is correct |
13 |
Correct |
10 ms |
70236 KB |
Output is correct |
14 |
Correct |
10 ms |
70224 KB |
Output is correct |
15 |
Correct |
10 ms |
70416 KB |
Output is correct |
16 |
Correct |
10 ms |
70224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
70224 KB |
Output is correct |
2 |
Correct |
13 ms |
70372 KB |
Output is correct |
3 |
Correct |
10 ms |
70224 KB |
Output is correct |
4 |
Correct |
10 ms |
70428 KB |
Output is correct |
5 |
Correct |
10 ms |
70224 KB |
Output is correct |
6 |
Correct |
11 ms |
70224 KB |
Output is correct |
7 |
Correct |
11 ms |
70224 KB |
Output is correct |
8 |
Correct |
9 ms |
70224 KB |
Output is correct |
9 |
Correct |
9 ms |
70192 KB |
Output is correct |
10 |
Correct |
9 ms |
70224 KB |
Output is correct |
11 |
Correct |
9 ms |
70236 KB |
Output is correct |
12 |
Correct |
11 ms |
70224 KB |
Output is correct |
13 |
Correct |
10 ms |
70236 KB |
Output is correct |
14 |
Correct |
10 ms |
70224 KB |
Output is correct |
15 |
Correct |
10 ms |
70416 KB |
Output is correct |
16 |
Correct |
10 ms |
70224 KB |
Output is correct |
17 |
Correct |
116 ms |
126792 KB |
Output is correct |
18 |
Correct |
117 ms |
126848 KB |
Output is correct |
19 |
Correct |
114 ms |
127304 KB |
Output is correct |
20 |
Correct |
122 ms |
114760 KB |
Output is correct |
21 |
Correct |
127 ms |
112356 KB |
Output is correct |
22 |
Correct |
125 ms |
111632 KB |
Output is correct |
23 |
Correct |
132 ms |
94536 KB |
Output is correct |
24 |
Correct |
128 ms |
96200 KB |
Output is correct |
25 |
Correct |
129 ms |
114824 KB |
Output is correct |
26 |
Correct |
128 ms |
87492 KB |
Output is correct |
27 |
Correct |
118 ms |
91076 KB |
Output is correct |
28 |
Correct |
112 ms |
91076 KB |
Output is correct |
29 |
Correct |
106 ms |
106880 KB |
Output is correct |
30 |
Correct |
117 ms |
100684 KB |
Output is correct |
31 |
Correct |
101 ms |
120392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
70392 KB |
Output is correct |
2 |
Correct |
8 ms |
70248 KB |
Output is correct |
3 |
Correct |
9 ms |
70224 KB |
Output is correct |
4 |
Correct |
9 ms |
70224 KB |
Output is correct |
5 |
Correct |
8 ms |
70104 KB |
Output is correct |
6 |
Correct |
9 ms |
70392 KB |
Output is correct |
7 |
Correct |
201 ms |
115404 KB |
Output is correct |
8 |
Correct |
205 ms |
119464 KB |
Output is correct |
9 |
Correct |
194 ms |
118972 KB |
Output is correct |
10 |
Correct |
177 ms |
119488 KB |
Output is correct |
11 |
Correct |
173 ms |
123836 KB |
Output is correct |
12 |
Correct |
219 ms |
125116 KB |
Output is correct |
13 |
Correct |
39 ms |
72200 KB |
Output is correct |
14 |
Correct |
31 ms |
70736 KB |
Output is correct |
15 |
Correct |
191 ms |
128368 KB |
Output is correct |
16 |
Correct |
220 ms |
128544 KB |
Output is correct |
17 |
Correct |
191 ms |
128460 KB |
Output is correct |
18 |
Correct |
37 ms |
72016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
70224 KB |
Output is correct |
2 |
Correct |
13 ms |
70372 KB |
Output is correct |
3 |
Correct |
10 ms |
70224 KB |
Output is correct |
4 |
Correct |
10 ms |
70428 KB |
Output is correct |
5 |
Correct |
10 ms |
70224 KB |
Output is correct |
6 |
Correct |
11 ms |
70224 KB |
Output is correct |
7 |
Correct |
11 ms |
70224 KB |
Output is correct |
8 |
Correct |
9 ms |
70224 KB |
Output is correct |
9 |
Correct |
9 ms |
70192 KB |
Output is correct |
10 |
Correct |
9 ms |
70224 KB |
Output is correct |
11 |
Correct |
9 ms |
70236 KB |
Output is correct |
12 |
Correct |
11 ms |
70224 KB |
Output is correct |
13 |
Correct |
10 ms |
70236 KB |
Output is correct |
14 |
Correct |
10 ms |
70224 KB |
Output is correct |
15 |
Correct |
10 ms |
70416 KB |
Output is correct |
16 |
Correct |
10 ms |
70224 KB |
Output is correct |
17 |
Correct |
116 ms |
126792 KB |
Output is correct |
18 |
Correct |
117 ms |
126848 KB |
Output is correct |
19 |
Correct |
114 ms |
127304 KB |
Output is correct |
20 |
Correct |
122 ms |
114760 KB |
Output is correct |
21 |
Correct |
127 ms |
112356 KB |
Output is correct |
22 |
Correct |
125 ms |
111632 KB |
Output is correct |
23 |
Correct |
132 ms |
94536 KB |
Output is correct |
24 |
Correct |
128 ms |
96200 KB |
Output is correct |
25 |
Correct |
129 ms |
114824 KB |
Output is correct |
26 |
Correct |
128 ms |
87492 KB |
Output is correct |
27 |
Correct |
118 ms |
91076 KB |
Output is correct |
28 |
Correct |
112 ms |
91076 KB |
Output is correct |
29 |
Correct |
106 ms |
106880 KB |
Output is correct |
30 |
Correct |
117 ms |
100684 KB |
Output is correct |
31 |
Correct |
101 ms |
120392 KB |
Output is correct |
32 |
Correct |
9 ms |
70392 KB |
Output is correct |
33 |
Correct |
8 ms |
70248 KB |
Output is correct |
34 |
Correct |
9 ms |
70224 KB |
Output is correct |
35 |
Correct |
9 ms |
70224 KB |
Output is correct |
36 |
Correct |
8 ms |
70104 KB |
Output is correct |
37 |
Correct |
9 ms |
70392 KB |
Output is correct |
38 |
Correct |
201 ms |
115404 KB |
Output is correct |
39 |
Correct |
205 ms |
119464 KB |
Output is correct |
40 |
Correct |
194 ms |
118972 KB |
Output is correct |
41 |
Correct |
177 ms |
119488 KB |
Output is correct |
42 |
Correct |
173 ms |
123836 KB |
Output is correct |
43 |
Correct |
219 ms |
125116 KB |
Output is correct |
44 |
Correct |
39 ms |
72200 KB |
Output is correct |
45 |
Correct |
31 ms |
70736 KB |
Output is correct |
46 |
Correct |
191 ms |
128368 KB |
Output is correct |
47 |
Correct |
220 ms |
128544 KB |
Output is correct |
48 |
Correct |
191 ms |
128460 KB |
Output is correct |
49 |
Correct |
37 ms |
72016 KB |
Output is correct |
50 |
Correct |
216 ms |
129096 KB |
Output is correct |
51 |
Correct |
217 ms |
128840 KB |
Output is correct |
52 |
Correct |
238 ms |
126792 KB |
Output is correct |
53 |
Correct |
236 ms |
124356 KB |
Output is correct |
54 |
Correct |
248 ms |
125572 KB |
Output is correct |
55 |
Correct |
239 ms |
128732 KB |
Output is correct |
56 |
Correct |
161 ms |
128840 KB |
Output is correct |
57 |
Correct |
170 ms |
122952 KB |
Output is correct |
58 |
Correct |
173 ms |
109896 KB |
Output is correct |