#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector t(n + 2, 0ll);
FOR (i, 1, n)
cin >> t[i];
const LL INF = LL(1e18);
t[0] = -INF;
t[n + 1] = INF;
debug(t);
const int Z = __lg(int(1e9)) + 2;
debug(Z);
vector type(n + 1, Z - 1);
REP (i, n + 1) {
REP (j, Z)
if ((1ll << j) > (t[i + 1] - t[i])) {
type[i] = j;
break;
}
}
debug(type);
vector prv(n + 1, vector(Z, 0));
auto nxt = prv;
REP (i, Z)
prv[1][i] = 1;
FOR (i, 2, n) {
prv[i] = prv[i - 1];
REP (j, Z)
if ((1 << j) <= t[i] - t[i - 1])
prv[i][j] = i;
}
REP (i, Z)
nxt[n][i] = n;
for (int i = n - 1; i >= 1; --i) {
nxt[i] = nxt[i + 1];
REP (j, Z)
if ((1 << j) <= t[i + 1] - t[i])
nxt[i][j] = i;
}
debug(prv);
debug(nxt);
int q;
cin >> q;
REP (xd, q) {
LL x;
cin >> x;
LL ans = 0;
int pos = int(upper_bound(t.begin(), t.end(), x) - t.begin());
if (t[pos] - x >= x - t[pos - 1])
--pos;
ans = abs(x - t[pos]);
int lf = pos, rg = pos;
debug(pos, t[pos] - t[pos - 1], t[pos + 1] - t[pos]);
int side = (t[pos] - t[pos - 1]) <= (t[pos + 1] - t[pos]);
int pot = 0;
while (lf != 1 || rg != n) {
debug(lf, rg, side);
while ((1 << pot) <= t[rg] - t[lf])
++pot;
debug(pot);
if (side) {
// left
ans += t[lf] - t[prv[lf][pot]];
lf = prv[lf][pot];
if (lf > 1 && t[lf] - t[lf - 1] <= t[rg + 1] - t[lf]) {
ans += t[lf] - t[lf - 1];
--lf;
}
else if (rg < n) {
ans += t[rg] - t[lf];
side ^= 1;
}
}
else {
// right
ans += t[nxt[rg][pot]] - t[rg];
rg = nxt[rg][pot];
if (rg < n && t[rg + 1] - t[rg] < t[rg] - t[lf - 1]) {
ans += t[rg + 1] - t[rg];
++rg;
}
else if (lf > 1) {
ans += t[rg] - t[lf];
side ^= 1;
}
}
}
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
976 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
996 KB |
Output is correct |
15 |
Correct |
1 ms |
1000 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
976 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
996 KB |
Output is correct |
15 |
Correct |
1 ms |
1000 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
68 ms |
70364 KB |
Output is correct |
18 |
Correct |
70 ms |
70252 KB |
Output is correct |
19 |
Correct |
69 ms |
70336 KB |
Output is correct |
20 |
Correct |
70 ms |
70364 KB |
Output is correct |
21 |
Correct |
70 ms |
70364 KB |
Output is correct |
22 |
Correct |
70 ms |
70344 KB |
Output is correct |
23 |
Correct |
69 ms |
70376 KB |
Output is correct |
24 |
Correct |
70 ms |
70360 KB |
Output is correct |
25 |
Correct |
70 ms |
70360 KB |
Output is correct |
26 |
Correct |
77 ms |
70360 KB |
Output is correct |
27 |
Correct |
71 ms |
70340 KB |
Output is correct |
28 |
Correct |
71 ms |
70336 KB |
Output is correct |
29 |
Correct |
68 ms |
70228 KB |
Output is correct |
30 |
Correct |
68 ms |
70220 KB |
Output is correct |
31 |
Correct |
69 ms |
70312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
201 ms |
72408 KB |
Output is correct |
8 |
Correct |
217 ms |
72336 KB |
Output is correct |
9 |
Correct |
209 ms |
72340 KB |
Output is correct |
10 |
Incorrect |
195 ms |
72252 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
976 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
996 KB |
Output is correct |
15 |
Correct |
1 ms |
1000 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
68 ms |
70364 KB |
Output is correct |
18 |
Correct |
70 ms |
70252 KB |
Output is correct |
19 |
Correct |
69 ms |
70336 KB |
Output is correct |
20 |
Correct |
70 ms |
70364 KB |
Output is correct |
21 |
Correct |
70 ms |
70364 KB |
Output is correct |
22 |
Correct |
70 ms |
70344 KB |
Output is correct |
23 |
Correct |
69 ms |
70376 KB |
Output is correct |
24 |
Correct |
70 ms |
70360 KB |
Output is correct |
25 |
Correct |
70 ms |
70360 KB |
Output is correct |
26 |
Correct |
77 ms |
70360 KB |
Output is correct |
27 |
Correct |
71 ms |
70340 KB |
Output is correct |
28 |
Correct |
71 ms |
70336 KB |
Output is correct |
29 |
Correct |
68 ms |
70228 KB |
Output is correct |
30 |
Correct |
68 ms |
70220 KB |
Output is correct |
31 |
Correct |
69 ms |
70312 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
316 KB |
Output is correct |
38 |
Correct |
201 ms |
72408 KB |
Output is correct |
39 |
Correct |
217 ms |
72336 KB |
Output is correct |
40 |
Correct |
209 ms |
72340 KB |
Output is correct |
41 |
Incorrect |
195 ms |
72252 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |