Pagini recente » Cod sursa (job #2502477) | Cod sursa (job #1419107) | Cod sursa (job #296595) | Cod sursa (job #909830) | Cod sursa (job #2754601)
#include <fstream>
#define fs "stramosi"
using namespace std;
ifstream f(fs".in");
ofstream g(fs".out");
int a[250001];
int main() {
int n,m,x,y,aux;
f>>n>>m;
bool ok = true;
for(int i = 1 ; i <= n ; ++i )
{f>>a[i];
if(a[i]!=i-1)
ok=false;
}
if(ok){
while(f>>x>>y)
g<<max(x-y,0)<<" ";
}
else
for(int i = 0 ; i < m ; ++i )
{ f>>x>>y;
aux=x;
while(aux>0 && y>0)
{
aux=a[aux];
y--;
}
g<<aux<<" ";}
return 0;
}