Cod sursa(job #2347450)

Utilizator AndreiTurcasTurcas Andrei AndreiTurcas Data 18 februarie 2019 20:00:04
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("stramosi.in");
ofstream g("stramosi.out");

int n, m, a[21][250001];

int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        int x; f >> x;
        a[0][i] = x;
    }
    for(int i = 1; (1<<i) <= n; ++i)
        for(int j = 1; j <= n; ++j)
        if(a[i-1][j])
            a[i][j] = a[i-1][a[i-1][j]];
    for(int i = 1; i <= m; ++i)
    {
        int x, y;
        f >> x >> y;
        for (int i = 0; i <= 18; ++i) {
            if ((1<<i) & y)
                 x = a[i][x];
        }
        g << x << "\n";
    }
}