Cod sursa(job #3184728)

Utilizator Dragu_AndiDragu Andrei Dragu_Andi Data 16 decembrie 2023 17:01:08
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int str[250001][19];

int main()
{
    int n, m;
    fin >> n >> m;
    for(int i=1; i<=n; i++)
        fin >> str[i][0];
    for(int j=1; j<=18; j++)
        for(int i=1; i<=n; i++)
            str[i][j]=str[str[i][j-1]][j-1];
    while(m--)
    {
        int q, p;
        fin >> p >> q;
        for(int j=18; j>=0; j--)
        if((1<<j)&q)
            p=str[p][j];
        fout << p << '\n';
    }
    return 0;
}