Cod sursa(job #2611276)

Utilizator maramihaliMara Mihali maramihali Data 6 mai 2020 17:02:55
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MAX = 250001;

int dp[18][MAX];
int n, m, x, y;

int main()
{
    in >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        in >> dp[0][i];
    }
    for(int i = 1; i < 18; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
        }
    }
    while(m--)
    {
        in >> x >> y;
        int j = 0;
        while(y)
        {
            if(y % 2 != 0)
            {
                x = dp[j][x];
            }
            y /= 2;
            j++;
        }
        out << x << "\n";
    }
    return 0;
}