Cod sursa(job #2968530)

Utilizator apocal1ps13Stefan Oprea Antoniu apocal1ps13 Data 21 ianuarie 2023 14:28:51
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.55 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
const int NMAX = 2e5 + 1e5 / 2;
const int LOG = 20;
int up[NMAX + 1][LOG];
int n, x, y, query;
int main() {
	cin >> n >> query;
	for (int i = 1; i <= n; i++)
		cin >> up[i][0];

	for (int j = 1; j < LOG; j++)
		for (int i = 1; i <= n; i++)
			up[i][j] = up[up[i][j - 1]][j - 1];

	while (query--) {
		cin >> x >> y;
		for (int i = LOG - 1; i >= 0; i--)
			if (y & (1 << i)) x = up[x][i];
		cout << x << '\n';
	}
	return 0;
}