Cod sursa(job #3036577)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 24 martie 2023 16:49:10
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream cin("stramosi.in");
ofstream cout("stramosi.out");

const int MAX = 25e4 + 1;

int dp[19][MAX] , n , q , x , y;

// dp[i][j] = al 2^i lea stramos al lui j

int main(){

    cin >> n >> q;

    for(int i = 1 ; i <= n ; i++){

        cin >> dp[0][i];
    }

    for(int i = 1 ; i < 19 ; i++){

        for(int j = 1 ; j <= n ; j++){

            dp[i][j] = dp[i-1][dp[i-1][j]];
        }
    }

    while(q--){

        cin >> x >> y;

        int counter = 0;

        while(y){

            if(y%2){

                x = dp[counter][x];
            }

            y/=2;

            counter++;
        }

        cout << x << '\n';
    }

    return 0;
}