제출 #1243064

#제출 시각아이디문제언어결과실행 시간메모리
1243064trimkus던전 (IOI21_dungeons)C++20
0 / 100
94 ms22596 KiB
#include "dungeons.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> S, P, W, L;
int N;
const int MAXN = 5e4 + 1;
vector<int> adj[MAXN + 1];
int lift[MAXN + 1][20];
ll to_root[MAXN + 1];
ll cost[MAXN + 1][20];
void init(int n, std::vector<int> s, std::vector<int> p, std::vector<int> w, std::vector<int> l) {
	N = n;
	S = s;
	P = p;
	W = w;
	L = l;
	S.push_back(0);
	// cout << vals.size() << endl;
	for (int i = 0; i < n; ++i) {
		adj[w[i]].push_back(i);
	}
	auto dfs = [&](auto& dfs, int i, ll now) -> void {
		now += S[i];
		to_root[i] = now;
		for (auto& u : adj[i]) {
			dfs(dfs, u, now);
		}
	};
	dfs(dfs, n, 0);
	// for (int i = 0; i < n; ++i) {
	// 	cerr << cost[i] << " ";
	// }
	// cerr << "\n";
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j < 20; ++j) {
			lift[i][j] = -1;
			cost[i][j] = 1e18;
		}
	}
	// for (int i = 0; i < n; ++i) {
	// 	if (cycle[i]) {
	// 		cerr << i << " ";
	// 	}
	// }
	// cerr << "\n";
	for (int i = 0; i < n; ++i) {
		if (S[W[i]] <= S[i]) {
			lift[i][0] = W[i];
			cost[i][0] = S[i];
		}
	}
	for (int j = 1; j < 20; ++j) {
		for (int i = 0; i < n; ++i) {
			if (lift[i][j - 1] == -1) continue;
			lift[i][j] = lift[lift[i][j - 1]][j - 1];
			cost[i][j] = cost[i][j - 1] + cost[lift[i][j - 1]][j - 1];
		}
	}
	// for (int j = 0; j < 5; ++j) {
	// 	for (int i = 0; i < n; ++i) {
	// 		cerr << lift[i][j] << " ";
	// 	}
	// 	cerr << "\n";
	// }
	// for (int j = 0; j < 5; ++j) {
	// 	for (int i = 0; i < n; ++i) {
	// 		cerr << cost[i][j] << " ";
	// 	}
	// 	cerr << "\n";
	// }
	return;
}


long long simulate(int x, int z) {
	ll res = z;
	ll tot = 0;
	// cout << x << " -> ";
	int it = 0;
	while (x != N) {
		while (x != N && res < S[x]) {
			res += S[x];
			x = L[x];
		}
		if (x == N) break;
		// cerr << x << " -> ";
		for (int i = 19; i >= 0; --i) {
			if (lift[x][i] == -1) continue;
			if (cost[x][i] + res < S[lift[x][i]]) continue;
			// cerr << "a\n";
			res += cost[x][i];
			x = lift[x][i];
		}
		if (x == N) break;

		// cerr << x << endl;
		// assert(S[x] > res);
		if (res < S[x]) {
			res += S[x];
			x = L[x];
		} else {
			res += S[x];
			x = W[x];
		}
	}
	// cout << res << " -> ";
	// cout << to_root[x] << "\n";
	// res += to_root[x];
	return res;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...