Pagini recente » Cod sursa (job #616739) | Cod sursa (job #1129455) | Cod sursa (job #2941170) | Cod sursa (job #1924384) | Cod sursa (job #3171430)
#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;
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;
for(int i = Pow2Max - 1; i >= 0; i--){
pow = (1 << i);
if(b & pow){
a = dp[i][a];
b ^= pow;
}
}
fout << a << '\n';
}
return 0;
}