Cod sursa(job #1182035)

Utilizator tttesterLaura Surcel tttester Data 4 mai 2014 15:37:43
Problema Stramosi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <math.h>
/*#include <conio.h>
#include <sys\timeb.h> */

using namespace std;

long N, M;
long A[250001][18];

int main(int argc, char **argv)
{
	//struct timeb start, end;
	ifstream in("stramosi.in");
    ofstream out("stramosi.out");
    
    // read from infile.txt using cin
    cin.rdbuf(in.rdbuf());
	cout.rdbuf(out.rdbuf());
	
	//ftime(&start);
	
	// take input (I)
	cin>>N>>M;
	
	for(int i = 1; i<=N; ++i)
		cin>>A[i][0];
	
	// process input
	int depth = log10(N) / log10(2);
	
	for(int i = 1; i<=N; ++i)
	{
		for(int j = 1; j<=depth && A[i][j-1]; ++j)
		{
			long temp = A[i][j-1];
			A[i][j] = A[temp][j-1];
		}
	}
	
	for(int i = 1; i<=M; ++i)
	{
		long q, p;
		cin>>q>>p;
		
		long j = 0, r = q;
		while(p)
		{
			int digit = p & 1;
			if (digit)
				r = A[r][j];
			if (r == 0)
			   break;
			p >>= 1;
			++j;
		}
		
		if(r == q)
			cout<<0;
		else
			cout<<r;
			
		if(i < M)
			cout<<"\n";
    }
    /*ftime(&end);
	
	cout<<"Time: \n"<<(1000.0 * (end.time - start.time)
        + (end.millitm - start.millitm))<<"\n";
	getch();*/
	return 0;
}