Pagini recente » Cod sursa (job #2423335) | Cod sursa (job #479030) | Cod sursa (job #1588218) | Cod sursa (job #2937671) | Cod sursa (job #2900652)
#include <bits/stdc++.h>
#define FILE "stramosi"
using namespace std;
ifstream fin(FILE".in");
ofstream fout(FILE".out");
const int maxN = 350000;
const int maxL = 20;
int m[maxN+5][maxL+5], arr[maxN+5];
int N,M,x,y;
int prelucrare(int x, int y){
int sol = x;
int c = 0;
while(y != 0){
c++;
if(y%2 == 1){
sol = m[sol][c-1];
}
y>>=2;
}
return sol;
}
int main(){
ios::sync_with_stdio(false);
fin >> N >> M;
m[0][0] = 0;
for(int i = 1; i <= N; ++i){
fin >> arr[i];
m[i][0] = arr[i];
}
for(int j = 1; j < maxL; ++j){
for(int i = 1; i <= N; ++i){
x = m[i][j-1];
m[i][j] = m[x][j-1];
}
}
/*
for(int i = 0 ; i <= N; ++i){
for(int j = 0 ; j <= N; ++j)
cout << m[i][j] << ' ';
cout << '\n';
}
*/
for(int i = 0; i < M; ++i){
fin >> x >> y;
fout << prelucrare(x,y) << '\n';
}
}