Pagini recente » Monitorul de evaluare | Cod sursa (job #2076046) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #1946)
Cod sursa(job #1946)
#include <stdio.h>
#include <math.h>
double doi=2;
using namespace std;
int main()
{
long (*a)[300100],*nrs,n,m,p,q,i,x[18]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072};
double j;
freopen ("stramosi.in","r",stdin);
freopen ("stramosi.out","w",stdout);
scanf ("%d %d",&n,&m);
nrs=new long[300100];
a=new long[19][300100];
a[0][0]=0;
for (i=1;i<=n;i++)
{
scanf ("%d",&a[0][i]);
nrs[i]=1;
if (i<18)
a[i][0]=0;
}
for (i=1;i<=n;i++)
for (j=1;j<18;j++)
{
int y=j;
a[y][i]=a[y-1][a[y-1][i]];
if (a[y][i])
nrs[i]++;
}
for (i=0;i<m;i++)
{
scanf("%d %d",&q,&p);
if (p>nrs[q])
printf("0\n");
else
{int y=16;
while (!(p&x[y])&&y>=0)
y--;
if (y<0)
printf("0\n");
while (x[y]!=p)
{
p-=x[y];
q=a[y][q];
y=16;
while (!(p&x[y])&&y>=0)
y--;
if (y<0)
printf("0\n");
}
printf("%d\n",a[y][q]);}
}
return 0;
}