Pagini recente » Monitorul de evaluare | Istoria paginii utilizator/patriciaxd | Statistici Roman Walker (pestcontrol660) | Monitorul de evaluare | Cod sursa (job #610419)
Cod sursa(job #610419)
#include<stdio.h>
#define N 250001
long n,m,i,x,y,v[N],j=0;
int main()
{freopen("stramosi.in","r",stdin);
freopen("stramosi.out","w",stdout);
scanf("%ld%ld",&n,&m);
for(i=1;i<=n;i++)
{scanf("%ld",&v[i]);
if(v[i]==i-1)
j++;}
if(j==n)
{while(m--)
{scanf("%ld%ld",&x,&y);
if(x>=y)
printf("%ld\n",x-y);
else
printf("0\n");}}
else
{while(m--)
{scanf("%ld%ld",&x,&y);
for(i=y;x&&i;i--)
x=v[x];
printf("%ld\n",x);}}
return 0;}