Cod sursa(job #487013)

Utilizator zloteanu.adrianzloteanu adrian nichita zloteanu.adrian Data 23 septembrie 2010 16:07:14
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.43 kb
#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;}