Pagini recente » Cod sursa (job #2281164) | Cod sursa (job #2247360) | Cod sursa (job #1215456) | Cod sursa (job #2199526) | Cod sursa (job #2968388)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");
const int Nmax=250000;
const short Pmax=17;
int n, m, dp[Nmax+1][Pmax+1], conv[]={1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072};
int main()
{
fin>>n>>m;
for (int i=1;i<=n;i++)
fin>>dp[i][0];
int p=1, j=1;
while (p<=n){
for (int i=1;i<=n;i++)
dp[i][j]=dp[dp[i][j-1]][j-1];
j++;
p<<=1;
}
int nod, anc;
for (int q=0; q<m; q++){
fin>>nod>>anc;
p=Pmax;
while (anc!=0){
while (conv[p]>anc)
p--;
nod=dp[nod][p];
anc-=conv[p];
}
fout<<nod<<'\n';
}
return 0;
}