Submission #773937

#TimeUsernameProblemLanguageResultExecution timeMemory
773937arush_aguBall Machine (BOI13_ballmachine)C++17
12.91 / 100
1098 ms25540 KiB
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #ifdef DEBUG #include <time.h> #endif #define all(a) (a).begin(), (a).end() #define rev(a) (a).rbegin(), (a).rend() #define F first #define S second int recur_depth = 0; #ifdef DEBUG #define dbg(x) \ { \ ++recur_depth; \ auto x_ = x; \ --recur_depth; \ cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \ << __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \ } #else #define dbg(x) #endif using namespace std; using namespace __gnu_pbds; typedef pair<int, int> ii; typedef long long ll; typedef long double ld; typedef pair<ll, ll> llll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pair<int, int>> vii; typedef vector<pair<int, ii>> viii; typedef vector<vii> vvii; typedef vector<viii> vviii; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pair<ll, ll>> vll; typedef vector<vll> vvll; typedef vector<bool> vb; template <class type1> using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag, tree_order_statistics_node_update>; template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template <typename T_container, typename T = typename enable_if< !is_same<T_container, string>::value, typename T_container::value_type>::type> ostream &operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = 1e9; const ld EPS = 1e-9; const int MAXLG = 22; void solve() { int n, q; cin >> n >> q; vi par(n); for (int &x : par) cin >> x, x--; int root = find(all(par), -1) - par.begin(); vvi child(n); for (int i = 0; i < n; i++) if (i != root) child[par[i]].push_back(i); vi mn_subtree(n, INF); vi depth(n, -1); depth[root] = 0; function<void(int, int)> f = [&](int u, int p) { mn_subtree[u] = u; for (int &v : child[u]) { depth[v] = depth[u] + 1; f(v, u); mn_subtree[u] = min(mn_subtree[u], mn_subtree[v]); } }; f(root, -1); vvi up(MAXLG, vi(n, -1)); up[0] = par; for (int j = 1; j < MAXLG; j++) for (int i = 0; i < n; i++) if (up[j - 1][i] != -1) up[j][i] = up[j - 1][up[j - 1][i]]; auto kth_anc = [&](int u, int k) { for (int i = MAXLG - 1; i >= 0; i--) if (k & (1 << i)) { u = up[i][u]; } return u; }; auto just_lca = [&](int u, int v) -> ii { bool swaped = 0; if (depth[u] < depth[v]) swaped = 1, swap(u, v); u = kth_anc(u, depth[u] - depth[v]); for (int i = MAXLG - 1; i >= 0; i--) if (up[i][u] != up[i][v]) { u = up[i][u]; v = up[i][v]; } if (swaped) return {v, u}; return {u, v}; }; auto s_cmp = [&](int u, int v) { auto [x, y] = just_lca(u, v); return mn_subtree[x] < mn_subtree[y]; }; set<int, decltype(s_cmp)> s(s_cmp); for (int i = 0; i < n; i++) if (child[i].empty()) s.insert(i); // cerr << s << "\n"; vb is_ball(n); vi cnt_cball(n); while (q--) { int t, x; cin >> t >> x; if (t == 1) { // cerr << "Type 1!\n"; for (int i = 1; i <= x; i++) { // cerr << "\tIter " << i << ":\n"; // cerr << "\t\tS: " << s << "\n"; auto u = *s.begin(); s.erase(s.begin()); is_ball[u] = 1; if (u != root) { if (++cnt_cball[par[u]] == child[par[u]].size()) s.insert(par[u]); } if (i == x) cout << u + 1 << "\n"; } } else { x--; is_ball[x] = 0; cnt_cball[par[x]]--; int res = 0; while (x != root && is_ball[par[x]]) { is_ball[x] = 1; cnt_cball[par[x]]++; x = par[x]; is_ball[x] = 0; if (x != root) cnt_cball[par[x]]--; res++; } s.insert(x); cout << res << "\n"; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); clock_t start = clock(); int test_cases = 1; // cin >> test_cases; while (test_cases--) solve(); #ifdef DEBUG cerr << fixed << setprecision(10) << "\nTime Taken: " << (double)(clock() - start) / CLOCKS_PER_SEC << "s\n"; #endif return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'void solve()':
ballmachine.cpp:181:35: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  181 |           if (++cnt_cball[par[u]] == child[par[u]].size())
      |               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:213:11: warning: unused variable 'start' [-Wunused-variable]
  213 |   clock_t start = clock();
      |           ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...