Pagini recente » Cod sursa (job #81061) | Cod sursa (job #3265674) | Cod sursa (job #1779398) | Cod sursa (job #1630527) | Cod sursa (job #2503235)
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n, q, dp[250010][20], nod, dist, sol;
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;
sol = nod;
for(int i=0; (1<<i)<=dist; i++) if((dist & (1 << i))) sol = dp[sol][i];
g << sol << '\n';
}
return 0;
}