Cod sursa(job #2913908)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 17 iulie 2022 21:11:49
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "stramosi";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");


int n, m;
int logs[250005];
int dp[250005][25];
int main() {

	ios_base::sync_with_stdio(false);
	cin.tie();

	fin >> n >> m;
	for (int i = 1; i <= n; ++i)
		fin >> dp[i][0];

	logs[1] = 0;
	for (int i = 2; i <= n; ++i)
		logs[i] = logs[i / 2] + 1;

	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= 20; ++j)
			dp[i][j] = dp[dp[i][j - 1]][j - 1];

	while (m--) {
		int x, y, ac;
		fin >> x >> y;
		int p = 0;
		while (y) {
			if (y & 1)
				x = dp[x][p];
			p++;
			y >>= 1;
		}
		fout << x << '\n';
	}

	return 0;
}