Pagini recente » Cod sursa (job #1591256) | Cod sursa (job #1826714) | Cod sursa (job #114889) | Cod sursa (job #2705664) | Cod sursa (job #2893995)
#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;
}