Cod sursa(job #2977246)

Utilizator AlexCroitoriuAlex Croitoriu AlexCroitoriu Data 11 februarie 2023 09:58:17
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int t[250001][19];
int main()
{
    int n, m;
    f >> n >> m;
    for (int i = 1; i <= n; i++)
        f >> t[i][0];
    for (int j = 1; j <= 18; j++)
    {
        for (int i = 1; i <= n; i++)
            t[i][j] = t[t[i][j - 1]][j - 1];
    }
    while (m--)
    {
        int q, p;
        f >> q >> p;
        for (int j = 18; j >= 0; j--)
        {
            if ((p >> j) & 1)
                q = t[q][j];
        }
        g << q << '\n';
    }
}