Cod sursa(job #770068)

Utilizator ionut_blesneagIonut Blesneag ionut_blesneag Data 21 iulie 2012 21:20:56
Problema Stramosi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.46 kb
#include<fstream>
using namespace std;

ifstream f("stramosi.in");
ofstream g("stramosi.out");

int n,nq,i,j,k;
int a[20][250001];
int p,q,x;

int main()
{f>>n>>nq;
for(i=1; i<=n; i++)
  f>>a[0][i];
  
for(i=1; i<=18; i++)
 for(j=1; j<=n; j++)
  a[i][j]=a[i-1][a[i-1][j]];
  
for(i=1; i<=nq; i++)
 {f>>p>>q;
  x=p;
  j=0;
  while(q)
     {if(q%2==1)
        x=a[j][x];
      j++;
      q=q/2;}  

  g<<x<<endl;   
         }  
return 0;    
}