Cod sursa(job #2893994)

Utilizator DooMeDCristian Alexutan DooMeD Data 26 aprilie 2022 23:43:23
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.52 kb
#include <bits/stdc++.h>
using namespace std;
const int nmax = 250000;
const int lgmax = 19;

int up[nmax+5][lgmax+5];

int main() {
	ifstream f("stramosi.in");
	ofstream g("stramosi.out");

	int n, m; f >> n >> m;
	for(int x=1; x<=n; x++) 
		f >> up[x][0];
	for(int p=1; p<=lgmax; p++)
		for(int i=1; i<=n; i++)
			up[i][p] = up[up[i][p-1]][p-1];
	for(int i=1; i<=m; i++) {
		int w, lv; f >> w >> lv;
		for(int p=lgmax; p>=0; p--)
			if(lv - (1<<p) >= 0) {
				w = up[w][p];
				lv -= (1<<p);
			}
		g << w << "\n";
	}
	return 0;
}