제출 #519037

#제출 시각아이디문제언어결과실행 시간메모리
519037athensclubBall Machine (BOI13_ballmachine)C++14
100 / 100
415 ms28200 KiB
#include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <queue> using namespace std; int findMinSubtree(int node, vector<int> children[], int minSubtree[]) { if (minSubtree[node] > 0) return minSubtree[node]; int temp = node; for (int child : children[node]) temp = min(temp, findMinSubtree(child, children, minSubtree)); minSubtree[node] = temp; return temp; } void visitOrdered(int node, vector<int> children[], vector<int> &order) { for (int child : children[node]) visitOrdered(child, children, order); order.push_back(node); } void findUp(int node, vector<int> children[], int parent[], vector<vector<int>> &up, int logN, int depth[]) { up[node][0] = parent[node] == 0 ? node : parent[node]; for (int i = 1; i <= logN; i++) up[node][i] = up[up[node][i - 1]][i - 1]; for (int child : children[node]) { depth[child] = depth[node] + 1; findUp(child, children, parent, up, logN, depth); } } int main() { int n, q; cin >> n >> q; int parent[n + 5]; int minSubtree[n + 5]; bool hasBall[n + 5]; vector<int> children[n + 5]; int root; for (int i = 1; i <= n; i++) { hasBall[i] = false; minSubtree[i] = 0; cin >> parent[i]; if (parent[i] == 0) root = i; else children[parent[i]].push_back(i); } findMinSubtree(root, children, minSubtree); for (int i = 1; i <= n; i++) { sort(children[i].begin(), children[i].end(), [&minSubtree](const int node1, const int node2) { return minSubtree[node1] < minSubtree[node2]; }); } int logN = (int)ceil(log2(n)); int depth[n + 5]; depth[root] = 0; vector<vector<int>> up(n + 5, vector<int>(logN + 1)); findUp(root, children, parent, up, logN, depth); vector<int> orderToNode, nodeToOrder(n + 5); visitOrdered(root, children, orderToNode); for (int i = 0; i < orderToNode.size(); i++) nodeToOrder[orderToNode[i]] = i; int r = 0; priority_queue<int, vector<int>, greater<int>> orderToFill; for (int i = 0; i < q; i++) { int t, k; cin >> t >> k; if (t == 1) { int lastFilled = -1; while (k > 0 && !orderToFill.empty()) { hasBall[orderToNode[orderToFill.top()]] = true; lastFilled = orderToNode[orderToFill.top()]; orderToFill.pop(); k--; } if (k == 0) { cout << lastFilled << endl; } else { for (int j = 0; j < k; j++) hasBall[orderToNode[r + j]] = true; r += k; cout << orderToNode[r - 1] << endl; } } else { int temp = logN; int tempNode = k; int target = k; while (temp >= 0 && target != root) { if (hasBall[up[tempNode][temp]]) { target = up[tempNode][temp]; tempNode = up[tempNode][temp]; } else { temp--; } } hasBall[target] = false; orderToFill.push(nodeToOrder[target]); cout << (depth[k] - depth[target]) << endl; } } }

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 0; i < orderToNode.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:115:40: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
  115 |             while (temp >= 0 && target != root)
      |                                 ~~~~~~~^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...