Pagini recente » Autentificare | Istoria paginii runda/ems2 | Istoria paginii utilizator/anaturcitu | Cod sursa (job #970143) | Cod sursa (job #2923325)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("bilift.in");
ofstream out ("bilift.out");
int dp[50002][20], n, parent[50002], LOG, m, x, y;
void bl(){
for(int i = 1; i <= n; i++){
dp[i][0] = parent[i];
for(int j = 1; j < LOG; j++)
dp[i][j] = dp[ dp[i][j-1] ][j-1];
}
}
void yes(int x, int y){
for(int j = LOG - 1; j >= 0; j--)
if(y >= (1 << j)){
x = dp[x][j];
y -= 1 << j;}
cout << x << '\n';
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> parent[i];
while ((1 << LOG) <= n) LOG++;
bl();
/* for(int i = 0; i < n; i++, cout << '\n')
for(int j = 0; j < LOG; j++)
cout << dp[i][j] << " ";
cout << endl;
*/
for(int i = 1; i <= m; i++){
cin >> x >> y;
yes(x, y);
}
}