Pagini recente » Cod sursa (job #1043435) | Cod sursa (job #2468758) | Cod sursa (job #2780450) | Cod sursa (job #269284) | Cod sursa (job #2503240)
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n, q, dp[250010][20], nod, dist;
int main()
{
ios_base::sync_with_stdio(false);
f.tie(0);
g.tie(0);
f >> n >> q;
for(int i=1; i<=n; i++) f >> 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++)
{ f >> nod >> dist;
for(int i=0; (1<<i)<=dist; i++) if((dist & (1 << i))) nod = dp[nod][i];
g << nod << '\n';
}
return 0;
}