Pagini recente » Cod sursa (job #2931222) | Cod sursa (job #56815) | Cod sursa (job #1503581) | Cod sursa (job #545608) | Cod sursa (job #3231816)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 250001;
const int LOGN = 18;
int s[LOGN + 1][NMAX];
int n;
void constructie_s(){
for(int i = 1; (1 << i) <= n; i++){
for(int j = 1; j <= n; j++){
s[i][j] = s[i - 1][s[i - 1][j]];
}
}
}
int interogare(int x, int nrs){
int i = 0;
while((1<<i) <= nrs){
if(nrs & (1 << i)){
x = s[i][x];
}
i++;
}
return x;
}
int main()
{
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int m;
fin >> n >> m;
for(int i = 1; i <= n; i++){
fin >> s[0][i];
}
constructie_s();
for(int i = 0; i < m; i++){
int x, nrs;
fin >> x >> nrs;
fout << interogare(x, nrs) << "\n";
}
fin.close();
fout.close();
return 0;
}