Cod sursa(job #3321989)

Utilizator Lex._.Lex Guiman Lex._. Data 12 noiembrie 2025 08:07:43
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>
#define NMAX 250001
#define LOG 20
using namespace std;

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

int stra[LOG][NMAX];

int up(int nod, int k)
{
    int putere2=0;
    while((1<<putere2)<=k) putere2++;
    while(putere2>=0)
    {
        if(k>=(1<<putere2))
        {
            nod=stra[putere2][nod];
            k-=(1<<putere2);
        }
        putere2--;
    }
    return nod;
}

int main()
{
    int n, m;
    in >> n >> m;
    for(int i=1; i<=n; i++)
    {
        in >> stra[0][i];
    }
    for(int e=1; e<LOG; e++)
    {
        for(int i=1; i<=n; i++)
        {
            stra[e][i]=stra[e-1][stra[e-1][i]];
        }
    }
    while(m--)
    {
        int q, p;
        in >> q >> p;
        out << up(q, p) << "\n";
    }

    return 0;
}