Cod sursa(job #3245633)

Utilizator PitigoiOlteanEmanuelPitigoi Oltean Emanuel PitigoiOlteanEmanuel Data 29 septembrie 2024 19:47:08
Problema Stramosi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

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


int  v[250005][100];


int n;



int main()
{
 int q;

    cin>>n>>q;
    int l=log2(n);
    l++;
    for(int i=1;i<=n;i++)
    {
        cin>>v[0][i];
    }


    for(int i=1;i<=l+1;i++)
    {

        for(int k=1;k<=n;k++)
        {
            v[i][k]=v[i-1][v[i-1][k]];

        }


    }


    for(int i=1;i<=q;i++)
    {
        int a,b;
        cin>>a>>b;
        int ans=0;
        int x,y;
        int cnt=21;
        for(int pas=l;pas>=0;pas--)
        {

            if(b & (1<<pas) )
            {

               a=v[pas][a];


            }
        }
        cout<<a<<'\n';
    }




    return 0;
}