Submission #1195929

#TimeUsernameProblemLanguageResultExecution timeMemory
1195929belgianbotDungeons Game (IOI21_dungeons)C++20
0 / 100
2 ms3656 KiB
#include "dungeons.h"
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 400001;
vector<bool> processed(MAX_N,false), is_processing(MAX_N, false), is_cycle(MAX_N, false);
vector<long long> cycle(MAX_N);
vector<int> s,p,w,l;
stack<int> q;
int n;

void proc(int i) {
	if (i == n) return;
	if (is_processing[i]) {
		while(!q.empty()) {
			int x = q.top(); q.pop();
			is_processing[x] = false;
			is_cycle[x] = true;
			if (x == i) break;
		}
		return;
	}
	if (processed[i]) return;
	is_processing[i] = true;
	processed[i] =true;
	q.push(i);
	proc(l[i]);
}
void init(int nn, vector<int> ss, vector<int> pp, vector<int> ww, vector<int> ll) {
	s = ss; p = pp; w = ww; l = ll; n = nn;
	for (int i = 0; i < n; i++) {
		if (!processed[i]) {
			proc(i);
			while (!q.empty()) {
				int x = q.top(); q.pop();
				is_processing[x] = false;
			}
		}
	}
	processed.clear();
	processed.resize(MAX_N,false);
	for (int i = 0; i < n; i++) {
		if (processed[i] || !is_cycle[i]) continue;
		int pos = i;
		long long sz = 0;
		while (!processed[pos]) {
			processed[pos] = true;
			q.push(pos);
			sz += p[pos];
			pos = l[pos];
		}
		while (!q.empty()) {
			int x = q.top(); q.pop();
			cycle[x] = sz;
		}
	}
	return;
}

long long simulate(int x, int z) {
	long long lim = s[x];
	long long st = z;
	if (st >= lim) return st+ lim;
	
	int pos = x;
	while (!is_cycle[pos] && st < lim && pos != n) {
		st += (long long)(p[pos]);
		pos = l[pos];
	}
	if (pos == n) return st;
	if (st >= lim) return st + lim;
	st += ((lim - st) / cycle[pos]) * cycle[pos];
	
	while (st < lim && pos != n) {
		st += (long long)(p[pos]);
		pos = l[pos];
	}
	if (pos == n) return st;
	return st + lim;
}

#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...