Pagini recente » Cod sursa (job #1351219) | Cod sursa (job #1195628) | Cod sursa (job #3273017) | Cod sursa (job #2658713) | Cod sursa (job #2503255)
#include <fstream>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int n, q, dp[250010][20], nod, dist;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for(int i=1; i<=n; i++) cin >> dp[i][0];
for(int i=1; i<=18; i++)
for(int j=1; j<=n; j++) dp[j][i] = dp[dp[j][i-1]][i-1];
for(int j=1; j<=q; j++)
{ cin >> nod >> dist;
for(int i=0; i<=18; i++) if(dist&(1<<i)) nod = dp[nod][i];
cout << nod << '\n';
}
return 0;
}