Cod sursa(job #3323831)

Utilizator Gerald123Ursan George Gerald123 Data 19 noiembrie 2025 23:05:34
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.64 kb
/// patratele
#include <bits/stdc++.h>
using namespace std;

#define MOD 9901

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

int i, n, m, lc[250010][25], j,k,x,y;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fin >> n >> m;
  for (i = 1; i <= n; i++)
    fin >> lc[i][0];
  int ln = log2(n);
  for (i = 1; i <= ln; i++)
    for (j = 1; j <= n; j++)
      lc[j][i] = lc[lc[j][i - 1]][i - 1];
  for (j = 1; j <= m; j++)
  {
    fin >> x >> y;
    i=ln;
    while(y && x!=0)
    {
      if(y>=(1<<i))
      {
        x=lc[x][i];
        y-=(1<<i);
      }
      i--;
    }
    fout<<x<<'\n';
  }

  return 0;
}