Cod sursa(job #2759916)

Utilizator un_fes_galbendaniel guba un_fes_galben Data 21 iunie 2021 13:39:28
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int mat[20][250005];
int v[40];
char buff[4096];
int pbuf=4095;
void readbuff()
{
    pbuf=0;
    fin.read(buff,4095);
}
int citire()
{
    int nr=0;
    if(pbuf==4095)
    {
        readbuff();
    }
    while(buff[pbuf]<'0'||buff[pbuf]>'9')
    {
        pbuf++;
        if(pbuf==4095)
        {
            readbuff();
        }
    }
    while(buff[pbuf]>='0'&&buff[pbuf]<='9')
    {
        nr=nr*10+buff[pbuf]-'0';
        pbuf++;
        if(pbuf==4095)
        {
            readbuff();
        }
    }
    return nr;
}
int main()
{
    int n,k,i,j,lg=-1,put2=1;
    n=citire();k=citire();
    for(i=1;i<=n;i++){
        mat[0][i]=citire();
    }
    while(put2<=n){
        lg++;
        put2*=2;
    }
    put2=1;
    for(i=1;i<=lg;i++){
        put2*=2;
        for(j=put2;j<=n;j++){
            mat[i][j]=mat[i-1][mat[i-1][j]];
        }
    }
    int p,q;
    for(i=1;i<=k;i++){
        q=citire();p=citire();
        int poz=1,pas=-1,stramos=q;
        while(p>0){
            v[poz]=p%2;
            poz++;p/=2;
        }
        pas=poz-2;
        for(j=poz-1;j>=1;j--){
            if(v[j]==1){
                stramos=mat[pas][stramos];
            }
            pas--;
        }
        fout<<stramos<<'\n';
    }
    return 0;
}