Cod sursa(job #3311770)

Utilizator mihaigeorgescuGeorgescu Mihai mihaigeorgescu Data 24 septembrie 2025 10:13:49
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.47 kb
#include <fstream>

using namespace std;
ifstream fcin("stramosi.in");
ofstream fout("stramosi.out");
int n,m,p[20][300000],x,k;
int main()
{
    fcin>>n>>m;
    for(int i=1; i<=n; i++)
        fcin>>p[0][i];
    for(int i=1; i<20; i++)
        for(int j=0; j<=n; j++)
            p[i][j]=p[i-1][p[i-1][j]];
    while(m--)
    {
        fcin>>x>>k;
        for(int i=30; i>=0; i--)
            if(k&(1<<i)) x=p[i][x];
        fout<<x<<'\n';
    }
    return 0;
}