Cod sursa(job #1887675)

Utilizator GoogalAbabei Daniel Googal Data 21 februarie 2017 18:34:33
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#include <vector>
#define nmax 250010

using namespace std;

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

int n,m,x,y,d[30][nmax];

int main()
{
    int i,j;
    fin>>n>>m;
    for(i=1;i<=n;i++)
        fin>>d[0][i];
    for(i=1;(1<<i)<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            d[i][j]=d[i-1][d[i-1][j]];
        }
    }
    for(i=1;i<=m;i++)
    {
        fin>>x>>y;
        for(j=0; (1<<j)<=y ;j++)
        {
            if(y&(1<<j))
                x=d[j][x];
        }
        fout<<x<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}