Cod sursa(job #2650168)

Utilizator adiaioanaAdia R. adiaioana Data 17 septembrie 2020 16:46:36
Problema Stramosi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int str[250100][18];
int main()
{
    int N,Q,x,P,k;
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
    cin>>N>>Q;
    for(int i=1; i<=N; ++i)
        cin>>str[i][0];

    for(int p=1; p<=17; ++p)
        for(int i=1; i<=N; ++i)
            str[i][p]=str[str[i][p-1]][p-1];

    while(Q--)
    {
        cin>>x>>P; /// P=5 | 101
        k=0;
        while(P)
        {
            if(P&1)
                x=str[x][k];
            P>>=1; k++;
        }
        cout<<x<<'\n';
    }

    return 0;
}