/*************************************
* author: marvinthang *
* created: 22.03.2023 15:18:27 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const long long INF = 1e18;
const int MAX = 2e5 + 5;
const int LOG = 20;
int N, Q;
long long X[MAX];
long long rmq[2][MAX][LOG];
void init(void) {
cin >> N;
X[0] = -INF;
X[N + 1] = INF;
FORE(i, 1, N) cin >> X[i];
FORE(i, 1, N) {
rmq[0][i][0] = 2 * X[i + 1] - X[i];
rmq[1][i][0] = 2 * X[i - 1] - X[i];
}
FOR(k, 1, LOG) FORE(i, 1, N - MASK(k) + 1) {
rmq[0][i][k] = max(rmq[0][i][k - 1], rmq[0][i + MASK(k - 1)][k - 1]);
rmq[1][i][k] = min(rmq[1][i][k - 1], rmq[1][i + MASK(k - 1)][k - 1]);
}
cin >> Q;
}
long long get_max0(int i, int j) {
if (j < i) return -INF;
int h = __lg(j - i + 1);
return max(rmq[0][i][h], rmq[0][j - MASK(h) + 1][h]);
}
long long get_min1(int i, int j) {
if (j < i) return INF;
int h = __lg(j - i + 1);
return min(rmq[1][i][h], rmq[1][j - MASK(h) + 1][h]);
}
long long go(long long s) {
int p = lower_bound(X + 1, X + 1 + N, s) - X;
int l, r;
long long res = 0;
bool left = true;
if (s - X[p - 1] <= X[p] - s) {
l = r = p - 1;
res = s - X[p - 1];
} else {
l = r = p;
res = X[p] - s;
}
while (l > 1 || r < N) {
if (left) {
int low = 1, high = l;
while (low <= high) {
int m = low + high >> 1;
if (get_max0(m, l - 1) <= X[r + 1]) high = m - 1;
else low = m + 1;
}
res += X[l] - X[low];
l = low;
if (r < N) res += X[++r] - X[l];
left = false;
} else {
int low = r, high = N;
while (low <= high) {
int m = low + high >> 1;
if (get_min1(r + 1, m) > X[l - 1]) low = m + 1;
else high = m - 1;
}
res += X[high] - X[r];
r = high;
if (l > 1) res += X[r] - X[--l];
left = true;
}
}
return res;
}
void process(void) {
while (Q--) {
int s; cin >> s;
cout << go(s) << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("travel");
init();
process();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message
travel.cpp: In function 'long long int go(long long int)':
travel.cpp:97:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | int m = low + high >> 1;
| ~~~~^~~~~~
travel.cpp:108:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
108 | int m = low + high >> 1;
| ~~~~^~~~~~
travel.cpp: In function 'int main()':
travel.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:130:2: note: in expansion of macro 'file'
130 | file("travel");
| ^~~~
travel.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:130:2: note: in expansion of macro 'file'
130 | file("travel");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
980 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 |
1008 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
980 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 |
1008 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
92 ms |
66436 KB |
Output is correct |
18 |
Correct |
86 ms |
66432 KB |
Output is correct |
19 |
Correct |
94 ms |
66420 KB |
Output is correct |
20 |
Correct |
86 ms |
66432 KB |
Output is correct |
21 |
Correct |
92 ms |
66308 KB |
Output is correct |
22 |
Correct |
91 ms |
66340 KB |
Output is correct |
23 |
Correct |
94 ms |
66444 KB |
Output is correct |
24 |
Correct |
85 ms |
66428 KB |
Output is correct |
25 |
Correct |
109 ms |
66364 KB |
Output is correct |
26 |
Correct |
105 ms |
66380 KB |
Output is correct |
27 |
Correct |
95 ms |
66416 KB |
Output is correct |
28 |
Correct |
85 ms |
66432 KB |
Output is correct |
29 |
Correct |
88 ms |
66272 KB |
Output is correct |
30 |
Correct |
95 ms |
66376 KB |
Output is correct |
31 |
Correct |
79 ms |
66376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
221 ms |
68436 KB |
Output is correct |
8 |
Correct |
245 ms |
68412 KB |
Output is correct |
9 |
Correct |
212 ms |
68448 KB |
Output is correct |
10 |
Correct |
229 ms |
68324 KB |
Output is correct |
11 |
Correct |
228 ms |
68432 KB |
Output is correct |
12 |
Correct |
224 ms |
68436 KB |
Output is correct |
13 |
Correct |
35 ms |
4272 KB |
Output is correct |
14 |
Correct |
23 ms |
1492 KB |
Output is correct |
15 |
Correct |
254 ms |
69408 KB |
Output is correct |
16 |
Correct |
227 ms |
69576 KB |
Output is correct |
17 |
Correct |
222 ms |
69436 KB |
Output is correct |
18 |
Correct |
36 ms |
4132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
980 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 |
1008 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
92 ms |
66436 KB |
Output is correct |
18 |
Correct |
86 ms |
66432 KB |
Output is correct |
19 |
Correct |
94 ms |
66420 KB |
Output is correct |
20 |
Correct |
86 ms |
66432 KB |
Output is correct |
21 |
Correct |
92 ms |
66308 KB |
Output is correct |
22 |
Correct |
91 ms |
66340 KB |
Output is correct |
23 |
Correct |
94 ms |
66444 KB |
Output is correct |
24 |
Correct |
85 ms |
66428 KB |
Output is correct |
25 |
Correct |
109 ms |
66364 KB |
Output is correct |
26 |
Correct |
105 ms |
66380 KB |
Output is correct |
27 |
Correct |
95 ms |
66416 KB |
Output is correct |
28 |
Correct |
85 ms |
66432 KB |
Output is correct |
29 |
Correct |
88 ms |
66272 KB |
Output is correct |
30 |
Correct |
95 ms |
66376 KB |
Output is correct |
31 |
Correct |
79 ms |
66376 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
221 ms |
68436 KB |
Output is correct |
39 |
Correct |
245 ms |
68412 KB |
Output is correct |
40 |
Correct |
212 ms |
68448 KB |
Output is correct |
41 |
Correct |
229 ms |
68324 KB |
Output is correct |
42 |
Correct |
228 ms |
68432 KB |
Output is correct |
43 |
Correct |
224 ms |
68436 KB |
Output is correct |
44 |
Correct |
35 ms |
4272 KB |
Output is correct |
45 |
Correct |
23 ms |
1492 KB |
Output is correct |
46 |
Correct |
254 ms |
69408 KB |
Output is correct |
47 |
Correct |
227 ms |
69576 KB |
Output is correct |
48 |
Correct |
222 ms |
69436 KB |
Output is correct |
49 |
Correct |
36 ms |
4132 KB |
Output is correct |
50 |
Correct |
473 ms |
70544 KB |
Output is correct |
51 |
Correct |
456 ms |
70520 KB |
Output is correct |
52 |
Correct |
416 ms |
70500 KB |
Output is correct |
53 |
Correct |
368 ms |
70688 KB |
Output is correct |
54 |
Correct |
367 ms |
70576 KB |
Output is correct |
55 |
Correct |
366 ms |
70516 KB |
Output is correct |
56 |
Correct |
160 ms |
70368 KB |
Output is correct |
57 |
Correct |
156 ms |
70336 KB |
Output is correct |
58 |
Correct |
159 ms |
70512 KB |
Output is correct |