#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;
int n;
vector<ll> d;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
d.assign(n, 0);
for (int i = 0; i < n; i++) cin >> d[i];
vector<int> li(n, -1), ri(n, -1);
set<pair<ll, 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<ll, 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<ll, 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 += d[ri[i]] - d[i], i = ri[i];
else ans += d[i] - d[li[i]], i = li[i];
}
ans += d[n - 1] - d[0];
//cout << " ";
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
348 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 |
0 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 |
348 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 |
38 ms |
5468 KB |
Output is correct |
18 |
Correct |
37 ms |
5460 KB |
Output is correct |
19 |
Correct |
37 ms |
5468 KB |
Output is correct |
20 |
Correct |
40 ms |
5468 KB |
Output is correct |
21 |
Correct |
37 ms |
5460 KB |
Output is correct |
22 |
Correct |
37 ms |
5468 KB |
Output is correct |
23 |
Correct |
38 ms |
5468 KB |
Output is correct |
24 |
Correct |
35 ms |
5468 KB |
Output is correct |
25 |
Correct |
35 ms |
5520 KB |
Output is correct |
26 |
Correct |
35 ms |
5460 KB |
Output is correct |
27 |
Correct |
35 ms |
5528 KB |
Output is correct |
28 |
Correct |
35 ms |
5468 KB |
Output is correct |
29 |
Correct |
40 ms |
5456 KB |
Output is correct |
30 |
Correct |
42 ms |
5596 KB |
Output is correct |
31 |
Correct |
40 ms |
5720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
76 ms |
7324 KB |
Output is correct |
8 |
Correct |
77 ms |
7332 KB |
Output is correct |
9 |
Correct |
77 ms |
7336 KB |
Output is correct |
10 |
Correct |
75 ms |
7508 KB |
Output is correct |
11 |
Correct |
75 ms |
7508 KB |
Output is correct |
12 |
Correct |
74 ms |
7368 KB |
Output is correct |
13 |
Correct |
30 ms |
4276 KB |
Output is correct |
14 |
Correct |
23 ms |
1620 KB |
Output is correct |
15 |
Correct |
76 ms |
8408 KB |
Output is correct |
16 |
Correct |
81 ms |
8448 KB |
Output is correct |
17 |
Correct |
76 ms |
8528 KB |
Output is correct |
18 |
Correct |
32 ms |
4280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
348 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 |
38 ms |
5468 KB |
Output is correct |
18 |
Correct |
37 ms |
5460 KB |
Output is correct |
19 |
Correct |
37 ms |
5468 KB |
Output is correct |
20 |
Correct |
40 ms |
5468 KB |
Output is correct |
21 |
Correct |
37 ms |
5460 KB |
Output is correct |
22 |
Correct |
37 ms |
5468 KB |
Output is correct |
23 |
Correct |
38 ms |
5468 KB |
Output is correct |
24 |
Correct |
35 ms |
5468 KB |
Output is correct |
25 |
Correct |
35 ms |
5520 KB |
Output is correct |
26 |
Correct |
35 ms |
5460 KB |
Output is correct |
27 |
Correct |
35 ms |
5528 KB |
Output is correct |
28 |
Correct |
35 ms |
5468 KB |
Output is correct |
29 |
Correct |
40 ms |
5456 KB |
Output is correct |
30 |
Correct |
42 ms |
5596 KB |
Output is correct |
31 |
Correct |
40 ms |
5720 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
452 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
76 ms |
7324 KB |
Output is correct |
39 |
Correct |
77 ms |
7332 KB |
Output is correct |
40 |
Correct |
77 ms |
7336 KB |
Output is correct |
41 |
Correct |
75 ms |
7508 KB |
Output is correct |
42 |
Correct |
75 ms |
7508 KB |
Output is correct |
43 |
Correct |
74 ms |
7368 KB |
Output is correct |
44 |
Correct |
30 ms |
4276 KB |
Output is correct |
45 |
Correct |
23 ms |
1620 KB |
Output is correct |
46 |
Correct |
76 ms |
8408 KB |
Output is correct |
47 |
Correct |
81 ms |
8448 KB |
Output is correct |
48 |
Correct |
76 ms |
8528 KB |
Output is correct |
49 |
Correct |
32 ms |
4280 KB |
Output is correct |
50 |
Correct |
96 ms |
9528 KB |
Output is correct |
51 |
Correct |
94 ms |
9584 KB |
Output is correct |
52 |
Correct |
94 ms |
9572 KB |
Output is correct |
53 |
Correct |
93 ms |
9552 KB |
Output is correct |
54 |
Correct |
94 ms |
9424 KB |
Output is correct |
55 |
Correct |
100 ms |
9564 KB |
Output is correct |
56 |
Correct |
82 ms |
9312 KB |
Output is correct |
57 |
Correct |
76 ms |
9520 KB |
Output is correct |
58 |
Correct |
84 ms |
9732 KB |
Output is correct |