Cod sursa(job #2755323)

Utilizator iustin.pericicaPericica Iustin iustin.pericica Data 26 mai 2021 23:39:10
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>

using namespace std;

int a[20][250001];

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

int main () {


    int n, m;
    fin>>n>>m;

    for ( int i = 1 ; i <= n ; ++i )fin >> a[0][i];

    int ln = 0;
    int power2 = 1;
    while (power2 <= n) {
        power2 *= 2;
        ++ln;
    }

    ln--;
    for (int i = 1 ; i <= ln ; ++i){
        for (int i1 = 1 ; i1 <= n ; ++i1){
            a[i][i1] = a[i - 1][a[i - 1][i1]];
        }
    }

    for (int i = 0 ; i < m ; ++i) {
        int x, y;
        fin>>x>>y;

        for (int i = 0 ; y ; y >>= 1, ++i)
            if (y & 1)
                x = a[i][x];

        fout<<x<<"\n";

    }

    return 0;
}