Cod sursa(job #1961387)

Utilizator netfreeAndrei Muntean netfree Data 11 aprilie 2017 08:56:50
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <bits/stdc++.h>

using namespace std;

const int N_MAX = 250000 + 5;

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

int n, m;
int str[22][N_MAX];

void calc_stramosi(){
    for(int pd=1;pd<20;++pd)
        for(int i=1;i<=n;++i)
            str[pd][i]=str[pd-1][str[pd-1][i]];
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i<=n; ++i){
        int aux; fin >> aux;
        if(aux != 0)
            str[0][i] = aux;
    }

    calc_stramosi();

    int a,k;

    while(m--){
        fin >> a >> k;
        for(int i = 0; i<=20; ++i)
            if(k&(1<<i))
                a = str[i][a];
        fout << a << "\n";
    }
    return 0;
}