Cod sursa(job #3245627)

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

using namespace std;

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


int  v[250005][100];


int n;



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


    for(int i=1;i<=30;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=(1<<20);pas>0;pas/=2)
        {
            cnt--;
            if(pas+ans<=b )
            {

               a=v[cnt][a];
               ans+=pas;
                if(a==0)
                {
                    break;
                }
            }
        }
        cout<<a<<'\n';
    }




    return 0;
}