Cod sursa(job #2893995)

Utilizator DooMeDCristian Alexutan DooMeD Data 26 aprilie 2022 23:43:50
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.51 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;
		int ans = 0;
		while(lv) {
			if(lv%2) w = up[w][ans];
			ans++;
			lv /= 2;
		}
		g << w << "\n";
	}
	return 0;
}