Cod sursa(job #2503235)

Utilizator FrostfireMagirescu Tudor Frostfire Data 2 decembrie 2019 19:04:43
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <fstream>

using namespace std;

ifstream f("stramosi.in");
ofstream g("stramosi.out");

int n, q, dp[250010][20], nod, dist, sol;

int main()
{
    ios_base::sync_with_stdio(false);
    f.tie(0);
    g.tie(0);
    f >> n >> q;
    for(int i=1; i<=n; i++) f >> dp[i][0];
    for(int i=1; i<=18; i++)
        for(int j=1; j<=n; j++) dp[j][i] = dp[dp[j][i-1]][i-1];
    for(int j=1; j<=q; j++)
        {   f >> nod >> dist;
            sol = nod;
            for(int i=0; (1<<i)<=dist; i++) if((dist & (1 << i))) sol = dp[sol][i];
            g << sol << '\n';
        }
    return 0;
}