Cod sursa(job #2771100)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 25 august 2021 13:02:43
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <bits/stdc++.h>
#define LOG 20
#define NMAX 250005

using namespace std;

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

int n, m;
int stramos[LOG][NMAX];

int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; i++)
        f >> stramos[0][i];

    for(int i = 1; i < LOG; i++)
        for(int j = 1; j <= n; j++)
            stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];

    while(m--)
    {
        int x, y;
        f >> x >> y;
        for(int i = LOG - 1; i >= 0; i--)
            if((1 << i) & y)
                x = stramos[i][x];
        g << x << "\n";
    }

    return 0;
}