#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> d(n), li(n, -1), ri(n, -1);
for (int i = 0; i < n; i++) cin >> d[i];
set<pair<int, int> > id;
for (int i = n - 2; i >= 0; i--)
{
auto it = id.upper_bound({ d[i], n });
ri[i + 1] = (it == id.begin() ? n - 1 : prev(it)->second);
pair<int, int> nw = { 2 * d[i] - d[i + 1], i };
while (!id.empty() && id.rbegin()->first >= nw.first) id.erase(*id.rbegin());
id.insert(nw);
}
ri[0] = n - 1;
id.clear();
for (int i = 0; i + 1 < n; i++)
{
auto it = id.upper_bound({ d[i + 1], n });
li[i] = (it == id.end() ? 0 : it->second);
pair<int, int> nw = { 2 * d[i + 1] - d[i], i + 1 };
while (!id.empty() && id.begin()->first <= nw.first) id.erase(*id.begin());
id.insert(nw);
}
li[n - 1] = 0;
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
int i = lower_bound(d.begin(), d.end(), x) - d.begin();
if (i == n || (i && x - d[i - 1] <= d[i] - x)) i--;
ll ans = abs(d[i] - x);
while (i && i != n - 1)
{
if (i != ri[i]) ans += (ll)(d[ri[i]] - d[i]), i = ri[i];
else ans += (ll)(d[i] - d[li[i]]), i = li[i];
}
ans += d[n - 1] - d[0];
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
50 ms |
2648 KB |
Output is correct |
18 |
Correct |
51 ms |
2796 KB |
Output is correct |
19 |
Correct |
50 ms |
2652 KB |
Output is correct |
20 |
Correct |
50 ms |
2652 KB |
Output is correct |
21 |
Correct |
49 ms |
2652 KB |
Output is correct |
22 |
Correct |
49 ms |
2652 KB |
Output is correct |
23 |
Correct |
50 ms |
2648 KB |
Output is correct |
24 |
Correct |
50 ms |
2648 KB |
Output is correct |
25 |
Correct |
46 ms |
2652 KB |
Output is correct |
26 |
Correct |
49 ms |
2652 KB |
Output is correct |
27 |
Correct |
45 ms |
2652 KB |
Output is correct |
28 |
Correct |
45 ms |
2652 KB |
Output is correct |
29 |
Correct |
58 ms |
2652 KB |
Output is correct |
30 |
Correct |
52 ms |
2652 KB |
Output is correct |
31 |
Correct |
53 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
87 ms |
4092 KB |
Output is correct |
8 |
Correct |
88 ms |
4312 KB |
Output is correct |
9 |
Correct |
90 ms |
4100 KB |
Output is correct |
10 |
Correct |
87 ms |
3984 KB |
Output is correct |
11 |
Correct |
86 ms |
4180 KB |
Output is correct |
12 |
Correct |
92 ms |
4056 KB |
Output is correct |
13 |
Correct |
35 ms |
2384 KB |
Output is correct |
14 |
Correct |
27 ms |
856 KB |
Output is correct |
15 |
Correct |
93 ms |
4384 KB |
Output is correct |
16 |
Correct |
88 ms |
4432 KB |
Output is correct |
17 |
Correct |
88 ms |
4584 KB |
Output is correct |
18 |
Correct |
33 ms |
2272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
50 ms |
2648 KB |
Output is correct |
18 |
Correct |
51 ms |
2796 KB |
Output is correct |
19 |
Correct |
50 ms |
2652 KB |
Output is correct |
20 |
Correct |
50 ms |
2652 KB |
Output is correct |
21 |
Correct |
49 ms |
2652 KB |
Output is correct |
22 |
Correct |
49 ms |
2652 KB |
Output is correct |
23 |
Correct |
50 ms |
2648 KB |
Output is correct |
24 |
Correct |
50 ms |
2648 KB |
Output is correct |
25 |
Correct |
46 ms |
2652 KB |
Output is correct |
26 |
Correct |
49 ms |
2652 KB |
Output is correct |
27 |
Correct |
45 ms |
2652 KB |
Output is correct |
28 |
Correct |
45 ms |
2652 KB |
Output is correct |
29 |
Correct |
58 ms |
2652 KB |
Output is correct |
30 |
Correct |
52 ms |
2652 KB |
Output is correct |
31 |
Correct |
53 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
87 ms |
4092 KB |
Output is correct |
39 |
Correct |
88 ms |
4312 KB |
Output is correct |
40 |
Correct |
90 ms |
4100 KB |
Output is correct |
41 |
Correct |
87 ms |
3984 KB |
Output is correct |
42 |
Correct |
86 ms |
4180 KB |
Output is correct |
43 |
Correct |
92 ms |
4056 KB |
Output is correct |
44 |
Correct |
35 ms |
2384 KB |
Output is correct |
45 |
Correct |
27 ms |
856 KB |
Output is correct |
46 |
Correct |
93 ms |
4384 KB |
Output is correct |
47 |
Correct |
88 ms |
4432 KB |
Output is correct |
48 |
Correct |
88 ms |
4584 KB |
Output is correct |
49 |
Correct |
33 ms |
2272 KB |
Output is correct |
50 |
Correct |
130 ms |
4844 KB |
Output is correct |
51 |
Correct |
108 ms |
4944 KB |
Output is correct |
52 |
Correct |
109 ms |
4944 KB |
Output is correct |
53 |
Correct |
106 ms |
4944 KB |
Output is correct |
54 |
Correct |
109 ms |
4888 KB |
Output is correct |
55 |
Correct |
107 ms |
4816 KB |
Output is correct |
56 |
Correct |
91 ms |
4688 KB |
Output is correct |
57 |
Correct |
94 ms |
4688 KB |
Output is correct |
58 |
Correct |
94 ms |
4948 KB |
Output is correct |