Pagini recente » Cod sursa (job #502028) | Cod sursa (job #1791690) | Cod sursa (job #2150375) | Cod sursa (job #667364) | Cod sursa (job #2966430)
#include <bits/stdc++.h>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
const int N = 2500001;
int up[18][N + 1];
int n, q;
int jump(int x, int y)
{
int nivel = 0;
while(y > 0)
{
if(y & 1)
{
x = up[nivel][x];
}
nivel++;
y >>= 1;
}
return x;
}
int main()
{
in >> n >> q;
for(int i = 1; i <= n; i++)
{
in >> up[0][i];
}
for(int i = 1; (1 << i) <= n; i++)
{
for(int j = 1; j <= n; j++)
{
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
for(int i = 1; i <= q; i++)
{
int x, y;
in >> x >> y;
out << jump(x, y) << '\n';
}
return 0;
}