Cod sursa(job #2321634)

Utilizator OldpugAlex Ionescu Oldpug Data 16 ianuarie 2019 13:42:29
Problema Stramosi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.27 kb
#include <fstream>
int g[25][250001],n,m,i,j,k,l;main(){std::ifstream o{"stramosi.in"};o>>n>>m;for(;++i<=n;)o>>g[0][i];for(k=1;(1<<k)<=n;++k)for(;++j<=n;)g[k][j]=g[k-1][g[k-1][j]];std::ofstream s{"stramosi.out"};while(m--){o>>i>>j;while(j){if(j&1)i=g[l][i];++l;j/=2;}s<<i<<'\n';}}