Pagini recente » Cod sursa (job #549218) | Cod sursa (job #724592) | Cod sursa (job #684711) | Cod sursa (job #1339470) | Cod sursa (job #1559715)
#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[11][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[1][i]);
for(int i = 1; i <= n; i++){
for(int j = 2; j <= 10; j++){
v[j][i] = v[1][v[j - 1][i]];
}
}
while(t--){
Read(q); Read(p);
while(p > 0 && q != 0){
if(p > 10){
q = v[10][q];
} else {
q = v[p][q];
}
p -= 10;
}
fout << q << "\n";
}
return 0;
}