Pagini recente » Cod sursa (job #1128716) | Cod sursa (job #3242596) | Cod sursa (job #153822) | Cod sursa (job #1976556) | Cod sursa (job #3297442)
#include <bits/stdc++.h>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
int str[250006][22];
int main() {
int n,m;
in>>n>>m;
for (int i=1;i<=n;i++) {
in>>str[i][0];
}
for (int j=1;(1<<j)<=n;j++) {
for (int i=1;i<=n;i++)
str[i][j]=str[str[i][j-1]][j-1];
}
for (int i=1;i<=m;i++) {
int nod,h;
in>>nod>>h;
for (int j=0;j<=20;j++) {
if (h&(1<<j)) {
nod=str[nod][j];
h-=(1<<j);
}
}
out<<nod<<" "<<endl;
}
}