Pagini recente » Cod sursa (job #2718798) | Cod sursa (job #2321614) | Cod sursa (job #2560680) | Cod sursa (job #2276942) | Cod sursa (job #3247165)
#include <fstream>
using namespace std;
const int nmax = 25 * 1e4;
const int logmax = 17;
int str[logmax + 1][nmax + 5];
int stramosi(int x, int poz) {
int cnt;
cnt = 0;
while (poz > 0){
if (poz % 2 == 1)
x = str[cnt][x];
cnt ++;
poz /= 2;
}
return x;
}
int main(){
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");
int n, m;
fin >> n >> m;
for (int i = 1; i <= n; i++){
fin >> str[0][i];
}
for (int i = 1; ( 1 << i ) <= n; i++)
for (int j = 1; j <= n; j++)
str[i][j] = str[i - 1][str[i - 1][j]];
while (m --){
int x, y;
fin >> x >> y;
fout << stramosi (x, y) << "\n";
}
return 0;
}