This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<typename... Args> // tuples
ostream& operator<<(ostream& os, tuple<Args...> t) {
apply([&](Args... args) { string dlm = "{"; ((os << dlm << args, dlm = ", "), ...); }, t);
return os << "}";
}
template<typename T, typename V> // pairs
ostream& operator<<(ostream& os, pair<T, V> p) { return os << "{" << p.f << ", " << p.s << "}"; }
template<class T, class = decltype(begin(declval<T>()))> // iterables
typename enable_if<!is_same<T, string>::value, ostream&>::type operator<<(ostream& os, const T& v) {
string dlm = "{";
for (auto& i : v) os << dlm << i, dlm = ", ";
return os << "}";
}
template <typename T, typename... V>
void printer(string pfx, const char *names, T&& head, V&& ...tail) {
int i = 0;
while (names[i] && names[i] != ',') i++;
constexpr bool is_str = is_same_v<decay_t<T>, const char*>;
if (is_str) cerr << " " << head;
else cerr << pfx, cerr.write(names, i) << " = " << head;
if constexpr (sizeof...(tail)) printer(is_str ? "" : ",", names + i + 1, tail...);
else cerr << endl;
}
#ifdef LOCAL
#define dbg(...) printer(to_string(__LINE__) + ": ", #__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
#include "fun.h"
/*
we can query distance and rooted subtree size
subtask 1 and 2: n^2
subtask 3: perfect binary tree, rooted at 0 with segtree indexing (kinda)
subtask 4: im pretty sure this is meant to be a star graph
repeatedly taking the diameter will give a valid tour trust
*/
int n, q;
vt<int> createFunTour(int N, int Q) {
n = N;
q = Q;
vt<int> out;
n++;
vt<int> used(n);
vt<pl> dp(2 * n);
vt<int> depth(n);
depth[1] = 0;
FOR (i, 2, n) depth[i] = depth[i / 2] + 1;
auto pull = [&] (int u) {
dp[u] = max({{depth[u], u}, dp[2 * u], dp[2 * u + 1]});
};
ROF (i, 1, n) pull(i);
auto die = [&] (int u) {
dp[u] = {};
while (u /= 2) pull(u);
};
dbg(depth);
dbg(dp);
int u = n - 1;
used[n - 1] = used[0] = true;
out.pb(u - 1);
die(u);
F0R (i, n - 2) {
int v = u;
pi best = max(dp[2 * u], dp[2 * u + 1]); // {dist, node with furthest dist}
while (true) {
if (used[v / 2]) break;
best = max(best, {depth[u] - depth[v / 2], v / 2});
best = max(best, {depth[u] - depth[v / 2] * 2 + dp[v ^ 1].f, dp[v ^ 1].s});
v /= 2;
}
cerr << "furthest node from " << u << " is " << best.s << endl;
used[best.s] = true;
out.pb(best.s - 1);
die(best.s);
u = best.s;
}
dbg(out);
return out;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |