Submission #726861

#TimeUsernameProblemLanguageResultExecution timeMemory
726861LittleFlowers__Fun Tour (APIO20_fun)C++17
0 / 100
30 ms38508 KiB
#include "fun.h" #include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif mt19937 rng(101202); // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int l,int r) { if (l > r) { cerr << l << " " << r << "\n"; } return l + rng() % (r - l + 1); } const int N = 100010; set<pair<int, int>> children[N * 4]; vector<int> createFunTour(int n, int q) { function<void(int)> build = [&](int s) { if (s >= n) return; build(2 * s + 1); build(2 * s + 2); children[s].insert({0, s}); for (const auto &[c, v] : children[2 * s + 1]) children[s].insert({c + 1, v}); for (const auto &[c, v] : children[2 * s + 2]) children[s].insert({c + 1, v}); }; build(0); vector<bool> visited(n); function getSubtree = [&](int u) -> pair<int, int> { while (children[u].size() && visited[children[u].rbegin()->second]) children[u].erase(*children[u].rbegin()); assert(children[u].size()); return *children[u].rbegin(); }; function getSubtree_Left = [&](int u) -> pair<int, int> { return getSubtree(u * 2 + 1); }; function getSubtree_Right = [&](int u) -> pair<int, int> { return getSubtree(u * 2 + 2); }; int currentVertex = 0; while (currentVertex * 2 + 1 < n) currentVertex = currentVertex * 2 + 1; visited[currentVertex] = true; vector<int> answer = {currentVertex}; for (int i = 1; i < n; ++i) { pair< int, // distance int // vertex > currentBest; currentBest = getSubtree(currentVertex); for (int u = currentVertex; u > 0; u /= 2) { int p = u / 2; auto thisBest = u == p * 2 + 1 ? getSubtree_Right(p) : getSubtree_Left(p); if (thisBest > currentBest) currentBest = thisBest; } visited[currentVertex = currentBest.second] = true; answer.push_back(currentVertex); } return answer; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...