Cod sursa(job #2147116)

Utilizator Alex18maiAlex Enache Alex18mai Data 28 februarie 2018 14:39:19
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>

using namespace std;

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

int str[20][250100];

int LOG[250100];

int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n , m;
    cin>>n>>m;

    for (int i=2; i<=n; i++){
        LOG[i] = LOG[i/2] + 1;
    }

    for (int i=1; i<=n; i++){
        cin>>str[0][i];
    }

    for (int bit = 1; bit <= LOG[n]; bit++){
        for (int i=1; i<=n; i++){
            str[bit][i] = str[bit-1][str[bit-1][i]];
        }
    }

    for (int i=1; i<=m; i++){
        int nod , rang;
        cin>>nod>>rang;

        while (rang){

            nod = str[LOG[rang]][nod];
            rang -= (1 << LOG[rang]);
        }

        cout<<nod<<'\n';
    }

    return 0;
}