Cod sursa(job #2694478)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 9 ianuarie 2021 13:23:53
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <cstdio>

using namespace std;

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

#define MAXN 250000
#define MAXM 300000
#define log2M 18

int stramos[log2M + 1][MAXN + 1];

int cautstramos( int persoana, int nr_stramos ) {
  int i;
  i = 0;
  while ( nr_stramos > 0 ) {
      if ( nr_stramos % 2 ) {
        persoana = stramos[i][persoana];
      }
      i++;
      nr_stramos /= 2;
  }
  return persoana;
}

int main()
{
    int n, m, i, p2, x, y, j;
    cin >> n >> m;
    for ( i = 1; i <= n; i++ ) {
      cin >> stramos[0][i];
    }
    p2 = 1;
    i = 1;
    while ( p2 <= n ) {
      for ( j = 1; j <= n; j++ )
        stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];
      p2 = p2 * 2;
      i++;
    }
    for ( i = 0; i < m; i++ ) {
       cin >> x >> y;
       cout << cautstramos( x, y ) << "\n";
    }
    return 0;
}