Cod sursa(job #3247165)

Utilizator Luca_Miscocilucainfoarena Luca_Miscoci Data 5 octombrie 2024 22:53:39
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>

using namespace std;

const int nmax = 25 * 1e4;
const int logmax = 17;
int str[logmax + 1][nmax + 5];

int stramosi(int x, int poz) {
  int cnt;
  cnt = 0;
  while (poz > 0){
    if (poz % 2 == 1)
      x = str[cnt][x];
    cnt ++;
    poz /= 2;
  }
  return x;
}
int main(){

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

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

  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]];

  while (m --){
    int x, y;
    fin >> x >> y;
    fout << stramosi (x, y) << "\n";
  }
  return 0;
}