Cod sursa(job #585103)

Utilizator david95szabo david emanuel david95 Data 27 aprilie 2011 22:48:17
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.56 kb
#include<fstream> 
using namespace std; 
ifstream fin("stramosi.in"); 
ofstream fout("stramosi.out");
int n, m; 
int str[20][250001]; 
int main()
{ 
	int i, j, q, p, s; 
	fin >> n >> m; 
	for(i = 1; i <= n; ++i) 
	fin >> str[0][i]; 
	for(i = 1; (1 << i) < n; ++i) 
	for(j = 1; j <= n; ++j) 
	str[i][j] = str[i - 1][str[i - 1][j]]; 
	for(j = 1; j <= m; ++j) { 
	fin >> p >> q;
	i = 0; 
	while ( q )
	{ 
		if(q & 1) 
		p = str[i][p]; 
		q >>= 1; 
		++i; 
        
	} 
        
fout << p << '\n'; 
    
} 
    
fout.close(); 
  
return 0;    
}