Pagini recente » Cod sursa (job #1554061) | Cod sursa (job #572380) | Cod sursa (job #271426) | Cod sursa (job #357223) | Cod sursa (job #487013)
Cod sursa(job #487013)
#include<fstream>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
int n,m,a[18][250001];
int main()
{in>>n>>m;
int i=1,j;
for(;i<=n;++i)
in>>a[0][i];
for(j=1;j<18;++j)
for(i=1;i<=n;++i)
a[j][i]=a[j-1][a[j-1][i]];
for(;m;--m)
{int q,p,x=1,i=0;
in>>q>>p;
while(x<p)
{x<<=1;++i;}
while(x&&p&&q)
{if(x<=p)
{q=a[i][q];
p-=x;}
x>>=1;
--i;}
out<<q<<'\n';}
return 0;}