Cod sursa(job #2904795)

Utilizator Valentin06Maftei Valentin Valentin06 Data 18 mai 2022 01:59:07
Problema Stramosi Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

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

int matrice[20][250001];
int main()
{
    int nrStramosi, pStramos;
    in >> nrStramosi >> pStramos;
    for (int i = 1; i <= nrStramosi; i++){
        in >> matrice[0][i];
    }
    for(int i = 1; i<= 18; i++)
        for(int j = 1; j <= nrStramosi; j++)
            matrice[i][j] = matrice[i-1][matrice[i-1][j]];
    for (int i = pStramos; i > 0; i--)
    {
        int curent, distanta, exponent = 0;
        in >> curent >> distanta;
        for (int j = distanta; j > 0; j--)
        {
            if (distanta % 2 == 1)
                curent = matrice[exponent][curent];
            exponent ++;
            distanta = distanta/2;
        }
        out << curent << '\n';
    }
    return 0;
}