Cod sursa(job #2919137)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 15 august 2022 21:23:52
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")

using namespace std;

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

const int MAX_N = 250005;
int n, q, nod, pw2, step;
int ancestor[19][MAX_N];

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr), fout.tie(nullptr);

    fin>>n>>q;
    for(int j=1; j<=n; j++)
        fin>>ancestor[0][j];

    for(int i=1; (1 << i)<=n; i++)
        for(int j=1; j<=n; j++)
            ancestor[i][j] = ancestor[i-1][ancestor[i-1][j]];

    while(q--){
        fin>>nod>>step;
        pw2 = 0;
        while(step){
            if(step & 1)
                nod = ancestor[pw2][nod];
            pw2++;
            step >>= 1;
        }
        fout<<nod<<"\n";
    }
    return 0;
}