Pagini recente » Cod sursa (job #472573) | Cod sursa (job #1471578) | Cod sursa (job #69015) | Cod sursa (job #2615627) | Cod sursa (job #2714622)
#include <fstream>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int mat[19][250000];
int main()
{
int temp, cnt, n, m, p, q;
fin >> n >> m;
for(int i = 1; i <= n; ++i)
{
fin >> mat[0][i];
}
for(int j = 1; j <= 18; ++j)
{
for(int i = 1; i <= n; ++i)
{
mat[j][i] = mat[j - 1][mat[j - 1][i]];
}
}
for(int i = 1; i <= m; ++i)
{
fin >> q >> p;
temp = 1 << 18;
cnt = 18;
while(temp)
{
if(temp & p)
{
q = mat[cnt][q];
}
temp >>= 1;
--cnt;
}
fout << q;
}
return 0;
}