Cod sursa(job #1619943)

Utilizator bullracerGabriel bullracer Data 28 februarie 2016 19:56:02
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <fstream>

#define LMAX 18
#define NMAX 250007

using namespace std;

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

int n, m, Dp[LMAX][NMAX];

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> Dp[0][i];
    for(int i = 1; (1 << i) <= n; ++i)
        for(int j = 1; j <= n; ++j)
            Dp[i][j] = Dp[i - 1][Dp[i - 1][j]];
    for(int i = 1; i <= m; ++i){
        int a, b;
        cin >> a >> b;
        for(int j = 0; (1 << j) <= b; ++j)
            if((1 << j) & b)
                a = Dp[j][a];
        cout << a << "\n";
    }
    return 0;
}