Cod sursa(job #2749481)

Utilizator LordNecrateBiowCuciureanu Dragos-Adrian LordNecrateBiow Data 6 mai 2021 20:07:00
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include <math.h>
#include <vector>
#include <limits.h>

using namespace std;

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

int str[19][250001];

int stramos(int persoana, int nr) 
{
    int i;
    i = 0;
    while (nr) 
    {
        if (nr % 2 == 1) 
        {
            persoana = str[i][persoana];
        }
        i++;
        nr /= 2;
    }
    return persoana;
}

int main()
{
    int n, m, x, y, s;
    fin >> n >> m;

    for (int i = 1; i <= n; i++) 
    {
        fin >> x;
        str[0][i] = x;
    }

    for (int i = 1; (1 << i) <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            str[i][j] = str[i - 1][str[i - 1][j]];
        }
    }

    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        s = stramos(x, y);
        fout << s << "\n";
    }
	return 0;
}