Cod sursa(job #2810510)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 29 noiembrie 2021 16:16:12
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int maxN = 255005, maxLg = 19;
int n, nq;
int s[maxLg + 5][maxN], pow[maxLg];

int main()
{
    ios::sync_with_stdio(false);
    fin >> n >> nq;
    pow[0] = 1;
    for(int i = 1; i <= maxLg; i++)
        pow[i] = pow[i - 1] * 2;
    for(int i = 1; i <= n; i++)
        fin >> s[0][i];
    for(int i = 1; pow[i] <= n; i++)
        for(int nod = 1; nod <= n; nod++)
            s[i][nod] = s[i - 1][s[i - 1][nod]];
    for(int i = 1; i <= nq; i++)
    {
        int nod, nr;
        fin >> nod >> nr;
        for(int j = 0; pow[j] <= nr; j++)
            if (nr & pow[j])
            nod = s[j][nod];
        fout << nod << '\n';
    }
    return 0;
}