Cod sursa(job #251966)

Utilizator bogdanhm999Casu-Pop Bogdan bogdanhm999 Data 3 februarie 2009 18:13:28
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.55 kb
#include <stdio.h>
#define N 250005
int n,m,i,j,L,x,y,t[20][N];
int main(){
  freopen("stramosi.in","r",stdin); freopen("stramosi.out","w",stdout);
  scanf("%d %d",&n,&m);
  L=0;while ((1<<(L+1))<=n)L++;
  for (i=1;i<=n;++i)scanf("%d",&t[0][i]);
  for (i=1;i<=L;++i)
    for (j=1;j<=n;++j)
      t[i][j]=t[i-1][t[i-1][j]];
  for (i=1;i<=m;++i){
    scanf("%d %d",&x,&y);
    L=0;while ((1<<(L+1))<=y)L++;
    while (y){
      x=t[L][x]; y-=1<<L;
      L=0;while ((1<<(L+1))<=y)L++;
    }
    printf("%d\n",x);
  }
return 0;
}