Submission #767985

#TimeUsernameProblemLanguageResultExecution timeMemory
767985marvinthangBitaro's travel (JOI23_travel)C++17
100 / 100
473 ms70688 KiB
/************************************* * 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 (stderr)

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");
      |  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...