Cod sursa(job #1778952)

Utilizator topala.andreiTopala Andrei topala.andrei Data 14 octombrie 2016 15:47:28
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
const int maxn=250001;
int stramosi[19][maxn];
int N,M;

int main()
{
    int i,j,x,y;
    f>>N>>M;
    for (i=1;i<=N;i++)
        f>>stramosi[0][i];
    for (i=1;i<=19;i++)
        for (j=1;j<=N;j++)
            stramosi[i][j]=stramosi[i-1][stramosi[i-1][j]];
    for (i=1;i<=M;i++)
    {
        f>>x>>y;
        j=0;
        while(y)
        {
            if ((y&1)==1)
            {
                x=stramosi[j][x];
            }
            j++;
            y=y>>1;
        }
        g<<x<<'\n';
    }
    return 0;
}