Pagini recente » Cod sursa (job #2315374) | Cod sursa (job #190353) | Cod sursa (job #2566759) | Cod sursa (job #412405) | Cod sursa (job #1559711)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
const int NMax = 250005;
const int BMax = 1e5 + 5;
int v[NMax];
int pos = BMax - 1;
char Buffer[BMax];
inline void Read(int &x){
while(!isdigit(Buffer[pos])){
if(++pos == BMax){
fin.read(Buffer, BMax);
pos = 0;
}
}
x = 0;
while(isdigit(Buffer[pos])){
x = x * 10 + (Buffer[pos] - '0');
if(++pos == BMax){
fin.read(Buffer, BMax);
pos = 0;
}
}
}
int main(){
int n, t, q, p;
Read(n); Read(t);
for(int i = 1; i <= n; i++) Read(v[i]);
while(t--){
Read(q); Read(p);
while(p > 0 && q != 0){
q = v[q];
p--;
}
fout << q << "\n";
}
return 0;
}