Submission #1106023

#TimeUsernameProblemLanguageResultExecution timeMemory
1106023monaxiaBall Machine (BOI13_ballmachine)C++17
100 / 100
161 ms33736 KiB
// credit: Mai Hong Kien #include <bits/stdc++.h> #define pb push_back #define ppb pop_back #define fr first #define sc second #define all(v) v.begin(), v.end() #define mod (long long)(1e9 + 7) #define eps (long long)(1e-9) using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; ll LIMIT = 2e3 + 5; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll random (ll l, ll r){return uniform_int_distribution <ll> (l, r)(rng);} const ll Mod = 1e9 + 7; vector <int> min_index(1e5 + 1), euler; vector <vector <int>> p(1e5 + 1, vector <int> (30, -1)); void init(int n){ for(int j = 1; (1 << j) < n; j ++){ for(int i = 1; i <= n; i ++){ if(p[i][j - 1] != -1) p[i][j] = p[p[i][j - 1]][j - 1]; } } } bool comp(int& a, int& b){ return min_index[a] > min_index[b]; } void init_min(int node, vector <vector <int>>& graph, vector <int>& min_index){ for(auto& x : graph[node]){ init_min(x, graph, min_index); min_index[node] = min(min_index[node], min_index[x]); } } void dfs_euler(int node, vector <vector <int>>& graph){ euler.pb(node); for(auto& x : graph[node]) dfs_euler(x, graph); } void solve(){ int n, q, start = 1, cnt = 0; cin >> n >> q; vector <vector <int>> graph(n + 1); vector <int> id(n + 1), ball(n + 1, 0); for(int i = 1; i <= n; i ++){ cin >> p[i][0]; if(p[i][0]) graph[p[i][0]].pb(i); else start = i; } init(n); for(int i = 1; i <= n; i ++) min_index[i] = i; init_min(start, graph, min_index); for(int i = 1; i <= n; i ++) sort(all(graph[i]), comp); dfs_euler(start, graph); reverse(all(euler)); set <pair <int, int>> s; for(int i = 0; i < euler.size(); i ++) s.insert({i + 1, euler[i]}), id[euler[i]] = i + 1; while(q --){ int type, x; cin >> type >> x; if(type == 1){ while(x --){ auto it = s.begin(); ball[it -> sc] = 1; if(!x) cout << it -> sc << "\n"; s.erase(it); } continue; } if(type == 2){ int ans = 0; int op_cnt = 0; while(ball[p[x][0]]){ int i = __lg(n), cur = 1 << __lg(n); while(p[x][i] == -1 || !ball[p[x][i]]) i --, cur >>= 1; x = p[x][i]; ans += cur; } s.insert({id[x], x}); ball[x] = 0; cout << ans << "\n"; } } } signed main() { cin.tie(0)->sync_with_stdio(0); if(fopen("TOY.inp", "r")){ freopen("TOY.inp", "r", stdin); freopen("TOY.out", "w", stdout); } // cout << 1; return 0; ll n = 1; // cin >> n; while(n) { solve(); n --; cout << "\n"; } // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; }

Compilation message (stderr)

ballmachine.cpp: In function 'void solve()':
ballmachine.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i = 0; i < euler.size(); i ++) s.insert({i + 1, euler[i]}), id[euler[i]] = i + 1;
      |                    ~~^~~~~~~~~~~~~~
ballmachine.cpp:99:17: warning: unused variable 'op_cnt' [-Wunused-variable]
   99 |             int op_cnt = 0;
      |                 ^~~~~~
ballmachine.cpp:52:26: warning: unused variable 'cnt' [-Wunused-variable]
   52 |     int n, q, start = 1, cnt = 0;
      |                          ^~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen("TOY.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         freopen("TOY.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...