Pagini recente » Cod sursa (job #1548064) | Istoria paginii runda/wellcodesimulare2martieclasa11-12/clasament | Cod sursa (job #172641) | Cod sursa (job #916568) | Cod sursa (job #2893994)
#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;
for(int p=lgmax; p>=0; p--)
if(lv - (1<<p) >= 0) {
w = up[w][p];
lv -= (1<<p);
}
g << w << "\n";
}
return 0;
}