Submission #444924

#TimeUsernameProblemLanguageResultExecution timeMemory
444924aryan12Ball Machine (BOI13_ballmachine)C++17
100 / 100
244 ms40596 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 1e5 + 5;
vector<int> g[N];
int parent[N], minInSubtree[N];
int ranking[N], cnt = 1;
int revRanking[N];
bool isThereBoulder[N];
int DP[20][N];

void dfs(int node) {
	minInSubtree[node] = node;
	for(int i = 0; i < g[node].size(); i++) {
		dfs(g[node][i]);
		minInSubtree[node] = min(minInSubtree[node], minInSubtree[g[node][i]]);
	}
}

void dfs2(int node) {
	int pos, minVal = INT_MAX;
	vector<pair<int, int> > temp;
	for(int i = 0; i < g[node].size(); i++) {
		temp.push_back({minInSubtree[g[node][i]], i});
	}
	sort(temp.begin(), temp.end());
	for(int i = 0; i < temp.size(); i++) {
		dfs2(g[node][temp[i].second]);
	}
	ranking[node] = cnt++;
}

void ComputeDP(int n) {
	for(int i = 1; i < 20; i++) {
		for(int j = 1; j <= n; j++) {
			if(DP[i - 1][j] == -1)
				DP[i][j] = -1;
			else
				DP[i][j] = DP[i - 1][DP[i - 1][j]];
		}
	}
}

void Solve() {
	for(int i = 0; i < N; i++) {
		isThereBoulder[i] = false;
	}
	int n, q;
	cin >> n >> q;
	int root;
	for(int i = 1; i <= n; i++) {
		cin >> parent[i];
		DP[0][i] = parent[i];
		if(parent[i] == 0) {
			root = i;
			DP[0][i] = -1;
		}
		else {
			g[parent[i]].push_back(i);
		}
	}
	ComputeDP(n);
	dfs(root);
	dfs2(root);
	for(int i = 1; i <= n; i++) {
		//cout << ranking[i] << " ";
		revRanking[ranking[i]] = i;
	}
	//cout << "\n";
	priority_queue<int, vector<int>, greater<int> > pq;
	for(int i = 1; i <= n; i++) {
		pq.push(ranking[i]);
	}
	vector<int> ans;
	for(int i = 1; i <= q; i++) {
		int type;
		cin >> type;
		if(type == 1) {
			int boulders;
			cin >> boulders;
			while(boulders != 0) {
				boulders--;
				int curRank = pq.top();
				pq.pop();
				isThereBoulder[revRanking[curRank]] = true;
				if(boulders == 0) {
					ans.push_back(revRanking[curRank]);
				}
			}
		}
		else {
			int crushedNode, res = 0;
			cin >> crushedNode;
			for(int j = 19; j >= 0; j--) {
				if(DP[j][crushedNode] != -1 && isThereBoulder[DP[j][crushedNode]]) {
					crushedNode = DP[j][crushedNode];
					res += (1 << j);
				}
			}
			isThereBoulder[crushedNode] = false;
			pq.push(ranking[crushedNode]);
			ans.push_back(res);
		}
	}
	for(int i = 0; i < ans.size(); i++) {
		cout << ans[i] << "\n";
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
}

Compilation message (stderr)

ballmachine.cpp: In function 'void dfs(long long int)':
ballmachine.cpp:15:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'void dfs2(long long int)':
ballmachine.cpp:24:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
ballmachine.cpp:28:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for(int i = 0; i < temp.size(); i++) {
      |                 ~~^~~~~~~~~~~~~
ballmachine.cpp:22:6: warning: unused variable 'pos' [-Wunused-variable]
   22 |  int pos, minVal = INT_MAX;
      |      ^~~
ballmachine.cpp:22:11: warning: unused variable 'minVal' [-Wunused-variable]
   22 |  int pos, minVal = INT_MAX;
      |           ^~~~~~
ballmachine.cpp: In function 'void Solve()':
ballmachine.cpp:106:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |  for(int i = 0; i < ans.size(); i++) {
      |                 ~~^~~~~~~~~~~~
ballmachine.cpp:65:6: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   65 |  dfs2(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...