Pagini recente » Cod sursa (job #1326519) | Cod sursa (job #1160346) | Cod sursa (job #3163301) | Cod sursa (job #2541512) | Cod sursa (job #110346)
Cod sursa(job #110346)
#include<stdio.h>
#include <cstring>
int a[22][250010];
FILE *fin=fopen("stramosi.in","r"),
*fout=fopen("stramosi.out","w");
void readdata()
{
int x,ind,i=0,sem=1;
int LIM = 1700000;
char sir[1700000];
fgets(sir, LIM, fin); x = ind = 0;
int L=strlen(sir);
for (ind=0; ind<=L; ++ind)
{
if (sir[ind]==' ' || sir[ind]=='\n')
{
a[0][++i]=x;
x=0;
}
if (sir[ind]>='0' && sir[ind]<='9')
x = x*10+(sir[ind]-'0');
}
}
int main()
{
int n,m,i,j,p,q;
fscanf(fin,"%d%d\n",&n,&m);
//for(i=1;i<=n;i++)
// fscanf(fin,"%d",&a[0][i]);
readdata ();
//preprocesare
for(i=1;i<=19;i++)
for(j=1;j<=n;j++)
a[i][j]=a[i-1][a[i-1][j]];
int nb;
for(i=1;i<=m;i++)
{
fscanf(fin,"%d%d",&p,&q);
while(q)
{
nb=0;
while((1<<nb) <= q) nb++;
nb--;
q-=(1<<nb);
p=a[nb][p];
}
fprintf(fout,"%d\n",p);
}
fclose(fin);
fclose(fout);
return 0;
}