Submission #525425

#TimeUsernameProblemLanguageResultExecution timeMemory
525425LucaDantasDungeons Game (IOI21_dungeons)C++17
63 / 100
1845 ms738384 KiB
#include "dungeons.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 5e4+10, logn = 25; // colocar 25 ANIMALL
constexpr long long inf = 1e15L;

struct FunctionalGraph {
	int f[maxn][logn], mn[maxn][logn]; // p[i] == peso da aresta saindo de i, f[i] == aresta saindo de i
	long long tab[maxn][logn], cost[maxn][logn]; // s[i] == força necessaria para matar i

	void add_i(int i, int x, int v, long long st) { f[i][0] = x, cost[i][0] = v, mn[i][0] = i, tab[i][0] = st; }

	void build(int n) {
		f[n][0] = n;
		cost[n][0] = 0;
		mn[n][0] = n;

		/* for(int i = 0; i <= n; i++) {
				printf("(%d, %d | %lld) ", i, mn[i][0], tab[i][0]);	
		}
		puts(""); */

		for(int l = 1; l < logn; l++) {
			for(int i = 0; i <= n; i++) {
				f[i][l] = f[f[i][l-1]][l-1];
				cost[i][l] = cost[i][l-1] + cost[f[i][l-1]][l-1];

				if(tab[i][l-1] <= tab[ f[i][l-1] ][l-1] - cost[i][l-1])
					mn[i][l] = mn[i][l-1], tab[i][l] = tab[i][l-1];
				else {
					mn[i][l] = mn[ f[i][l-1] ][l-1];
					tab[i][l] = tab[f[i][l-1]][l-1] - cost[i][l-1];

					// printf("BR %d %lld\n", f[i][l-1], tab[f[i][l-1]][l-1]);
				}

				// printf("(%d, %d | %lld) ", i, mn[i][l], tab[i][l]);	
			}
			// puts("");
		}
		// puts("");
	}

	void go(int& x, long long& val) {
		if(val >= tab[x][0]) return;
		// printf("%d %lld %lld\n", x, val, tab[x][0]);
		for(int l = logn-1; l >= 0; l--)
			if(tab[x][l] > val) val += cost[x][l], x = f[x][l];
		// printf("%d %lld %lld\n", x, val, tab[x][0]);
		/* val += cost[x][0];
		x = f[x][0];
		printf("%d %lld %lld\n", x, val, tab[x][0]); */
		/* puts("");
		fflush(stdout); */
		assert(val >= tab[x][0]);
		// passo pro pai porque na binary lifting peguei o ultimo cara tal que o minimo é maior que o meu valor, então o prox é menor
	}
} graph[logn];

void init(int n, std::vector<int> s, std::vector<int> p, std::vector<int> w, std::vector<int> l) {
	for(int lg = 0; lg < logn; lg++) {
		for(int i = 0; i < n; i++) {
			if(s[i] <= (1<<lg)) graph[lg].add_i(i, w[i], s[i], inf);
			else graph[lg].add_i(i, l[i], p[i], s[i]);
		}
		graph[lg].build(n);
	}
	return;
}

long long simulate(int x, int z) {
	long long val = z;
	for(int lg = 0; lg < logn; lg++)
		graph[lg].go(x, val);
	return val;
}
#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...