Cod sursa(job #2182889)

Utilizator infomaxInfomax infomax Data 22 martie 2018 18:09:32
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;

ifstream F("stramosi.in");
ofstream G("stramosi.out");

int n, m, d[20][250005], x, y, j;

int main()
{
    F >> n >> m;
    for(int i = 1; i <= n; ++ i){
        F >> d[0][i];
    }
    for(int i = 1; i <= 18; ++ i)
        for(int j = 1; j <= n; ++ j){
            d[i][j] = d[i-1][d[i-1][j]];
        }
    while(m--){
        F >> x >> y;
        j = 0;
        while(y){
            if(y%2){
                x=d[j][x];
            }
            j++;
            y/=2;
        }
        G << x<<'\n';
    }
    return 0;
}