Cod sursa(job #2696265)

Utilizator teodorescunicolasteodorescu nicolas alexandru teodorescunicolas Data 15 ianuarie 2021 16:55:58
Problema Stramosi Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#define NMAXX 250000
#define PUTMAXX 17

FILE *fin, *fout;
int mat[PUTMAXX + 1][NMAXX + 1];
int readInt() {
  int ch, res = 0, semn = 1;
  while ( isspace( ch = fgetc( fin ) ) );
  do
    res = 10 * res + ch - '0';
  while ( isdigit( ch = fgetc( fin ) ) );
  return res;
}
int main() {
    int n, m, i, l, c, x, poz, baza2;
    fin = fopen( "stramosi.in", "r" );
    fout = fopen( "stramosi.out", "w" );
    n = readInt();
    m = readInt();
    for ( i = 1; i <= n; i++ ) {
        mat[0][i] = readInt();
    }
    for ( l = 1; ( 1 << l ) <= n; l++ ) {
        for ( c = 1; c <= n; c++ ) {
            mat[l][c] = mat[l - 1][mat[l - 1][c]];
        }
    }
    for ( i = 0; i < m; i++ ) {
        x = readInt();
        poz = readInt();
        baza2 = 0;
        while ( poz > 0 ) {
            if ( poz % 2 == 1 )
                x = mat[baza2][x];
            baza2++;
            poz /= 2;
        }
        fprintf( fout, "%d\n", x );
    }
    fclose( fin );
    fclose( fout );
    return 0;
}