Cod sursa(job #3322730)

Utilizator dragos_poputepopute dragos mihai dragos_popute Data 15 noiembrie 2025 13:57:00
Problema Stramosi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n,m,tt[250001];
int mat[250001][30];
void citire()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
        fin>>mat[0][i];
}
void matr()
{
    for(int i=1;(1<<i)<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {

                mat[i][j]=mat[i-1][mat[i-1][j]];

        }
    }
}

void solve()
{
    for(int i=1;i<=m;i++)
    {   int p,q;
        fin>>q>>p;
        while(p)
        {
            int l=log2(p);
            int z=(1<<l);
            q=mat[l][q];
            p-=z;

        }
        fout<<q;fout<<endl;

    }
}
int main()
{
    citire();
    matr();
    solve();
    return 0;
}