Cod sursa(job #2955477)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 17 decembrie 2022 09:24:10
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("stramosi.in");
ofstream fout("stramosi.out");

const int NMAX = 25e4;
const int LGMAX = 18;

int n, m;
int par[LGMAX][NMAX + 1];

int main() {
	ios_base :: sync_with_stdio(false);

	fin >> n >> m;

	for(int i = 1; i <= n; i++) {
		fin >> par[0][i];
	}

	for(int i = 1; (1 << i) < n; i++) {
		for(int j = 1; j <= n; j++) {
			par[i][j] = par[i - 1][par[i - 1][j]];
		}
	}

	for(int tc = 1; tc <= m; tc++) {
		int q, p;
		fin >> q >> p;

		for(int i = 0; (1 << i) <= p; i++) {
			if(p & (1 << i)) {
				q = par[i][q];
			}
		}

		fout << q << '\n';
	}
	return 0;
}