Cod sursa(job #2842241)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 31 ianuarie 2022 13:56:24
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

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

const int logmax = 20,Nmax = 250005;
int str[logmax][Nmax],t[Nmax];
int n,m;

int main()
{
    in >> n >> m;
    for (int i = 1; i <= n; i++)
        in >> t[i];
    for (int i = 1; i <= n; i++)
        str[0][i] = t[i];
    for (int i = 1; i < logmax; i++)
        for (int j = 1; j <= n; j++)
            str[i][j] = str[i - 1][str[i - 1][j]];
    for (int i = 1; i <= m; i++)
    {
        int x,y;
        in >> x >> y;
        int p = x,pas = 18;
        while (pas != -1)
        {
            if (1 << pas <= y)
            {
                p = str[pas][p];
                y -= 1 << pas;
            }
            pas--;
        }
        out << p << '\n';
    }
    return 0;
}