제출 #65140

#제출 시각아이디문제언어결과실행 시간메모리
65140SpeedOfMagicBall Machine (BOI13_ballmachine)C++17
100 / 100
622 ms39724 KiB
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/ #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") template<typename T> using v = vector<T>; #define int long long typedef long double ld; typedef string str; typedef vector<int> vint; #define rep(a, l, r) for(int a = (l); a < (r); a++) #define pb push_back #define sz(a) ((int) a.size()) const long long inf = 4611686018427387903; //2^62 - 1 #if 0 //FileIO const string fileName = ""; ifstream fin ((fileName == "" ? "input.txt" : fileName + ".in" )); ofstream fout((fileName == "" ? "output.txt" : fileName + ".out")); #define get fin>> #define put fout<< #else #define get cin>> #define put cout<< #endif #define eol put endl #define printVar(a) put #a << ": " << a << endl; void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg) ;read(args...) ;} void print(){} template<typename Arg,typename... Args> void print(Arg arg,Args... args){put (arg)<<" ";print(args...);} void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg arg,Args... args){put (arg)<<" ";debug(args...);} int getInt(){int a; get a; return a;} //code goes here const int N = 1e5 + 1; vint g[N]; const int M = 20; int kthAncestor[N][M]; void calcAncestor(int cur, int par) { kthAncestor[cur][0] = par; int p = par; int k = 0; while (p != -1) { p = kthAncestor[p][k]; k++; kthAncestor[cur][k] = p; } for (int i : g[cur]) calcAncestor(i, cur); } int minInTree[N]; void calcMin(int cur) { minInTree[cur] = cur; for (int i : g[cur]) { calcMin(i); minInTree[cur] = min(minInTree[cur], minInTree[i]); } } int timCur = 1; int tim[N]; set<pair<int, int>> nxt; void calcOrder(int cur) { v<pair<int, int>> ord; for (int i : g[cur]) ord.pb({minInTree[i], i}); sort(ord.begin(), ord.end()); for (auto i : ord) calcOrder(i.second); tim[cur] = timCur++; nxt.insert({tim[cur], cur}); } bool hasBall[N]; void run() { int n, q; read(n, q); n++; int r = -1; rep(i, 1, n) { int p; get p; if (p == 0) r = i; else g[p].pb(i); } rep(i, 1, n) rep(j, 0, M) kthAncestor[i][j] = -1; calcAncestor(r, -1); calcMin(r); calcOrder(r); //for (auto i : nxt) debug(i.first, i.second); memset(&hasBall, 0, sizeof(hasBall)); for (; q; q--) { int t; get t; if (t == 1) { int k; get k; while (k) { auto d = *nxt.begin(); nxt.erase(d); hasBall[d.second] = 1; k--; if (k == 0) put d.second; } } else { int v; get v; int cur = v; int d = 0; for (int i = M - 1; i >= 0;) { if (!hasBall[kthAncestor[cur][i]]) i--; else { cur = kthAncestor[cur][i]; d += (1 << i); } } hasBall[cur] = 0; nxt.insert({tim[cur], cur}); put d; } eol; } } int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); return 0;}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...