Pagini recente » Cod sursa (job #1142065) | Cod sursa (job #3207973) | Cod sursa (job #1463027) | Cod sursa (job #1560457) | Cod sursa (job #2965893)
#include <fstream>
using namespace std;
const int Nmax = 250001;
const int Pow2Max = 20;
int dp[Pow2Max][Nmax];
int main(){
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, m, a, b, pow, sol;
fin >> n >> m;
for(int j = 1; j <= n; j++){
fin >> dp[0][j];
}
for(int i = 1; i < Pow2Max; i++){
for(int j = 1; j <= n; j++){
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
for(int h = 0; h < m; h++){
fin >> a >> b;
while(b){
sol = -1;
for(int i = 0; i < Pow2Max; i++){
pow = (1 << i);
if(b & pow){
sol = i;
}
}
a = dp[sol][a];
b -= (1 << sol);
}
fout << a << '\n';
}
return 0;
}