Pagini recente » Cod sursa (job #1567970) | Cod sursa (job #2979979) | Cod sursa (job #2690043) | Cod sursa (job #1882337) | Cod sursa (job #1778261)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
const int maxn=400000;
int stramosi[21][maxn];
int N,M;
int main()
{
int i,j,a,b;
f>>N>>M;
for (i=1;i<=N;i++)
f>>stramosi[1][i];
for (i=2;i<=19;i++)
for (j=1;j<=N;j++)
stramosi[i][j]=stramosi[1][stramosi[i-1][j]];
for (i=1;i<=M;i++)
{f>>a>>b;
if (a>19) g<<0<<'\n';
else g<<stramosi[b][a]<<'\n';}
}