Pagini recente » Rating Bertie Domann (koppa5964) | Cod sursa (job #2540845) | Cod sursa (job #797834) | Cod sursa (job #165518) | Cod sursa (job #2751149)
//
// main.cpp
// stramosi
//
// Created by Vlad Calomfirescu on 14.05.2021.
//
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int v[250000];
int main()
{
int n,m,x,y,cnt=0,r;
fin >> n >> m;
for(int i=1;i<=n;i++)
{
fin >> v[i];
if(v[i]==i-1)
cnt++;
}
if(cnt==n)
{
for(int i=1;i<=m;i++)
{
fin >> x >> y;
if(x>=y)
fout << x-y << endl;
else
fout << 0 << endl;
}
}
else
{
for(int i=1;i<=m;i++)
{
fin >> x >> y;
r=x;
while(y>0 && r>0)
{
r=v[r];
y--;
}
fout << r << endl;
}
}
return 0;
}