Pagini recente » Cod sursa (job #1362120) | Istoria paginii runda/01234556789 | Clasament drg | Cod sursa (job #1993176) | Cod sursa (job #848429)
Cod sursa(job #848429)
#include<fstream>
using namespace std;
int a[250001][21],i,j,n,m,x,y;
int main()
{
ifstream f("stramosi.in");
ofstream g("stramosi.out");
f >> n >> m;
for (i=1;i<=n;i++)
f >> a[i][0];
for (j=1;j<=16;j++)
for (i=1;i<=n;i++)
a[i][j]=a[a[i][j-1]][j-1];
for (i=1;i<=m;i++)
{
f >> x >> y;
for (j=0;y>0;j++)
{
if ((y & (1 << j))>0)
{
x=a[x][j];
y-=1 << j;
}
}
g << x << "\n";
}
return 0;
}