Cod sursa(job #3235446)

Utilizator stefanbejan07Bejan Stefan stefanbejan07 Data 17 iunie 2024 22:49:32
Problema Stramosi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
int Stramosi[250001];
int RMQ[250001][18];
int main()
{
	int N, NumberOfQueries;
	cin >> N >> NumberOfQueries;
	for (int i = 1; i <= N; i++)
		cin >> Stramosi[i];
	int LOG_MAX = (int)log2(N);
	for (int i = 1; i <= N; i++)
		RMQ[i][0] = Stramosi[i];
	for (int j = 1; j <= LOG_MAX; j++)
		for (int i = 1; i <= N; i++)
			RMQ[i][j] = RMQ[RMQ[i][j - 1]][j - 1];
	while (NumberOfQueries)
	{
		int Q, P;
		cin >> Q >> P;
		queue < bool > Bits;	
		int CopyP = P;
		while (CopyP != 0)
		{
			Bits.push(CopyP % 2);
			CopyP = CopyP / 2;
		}
		int BitCounter = 0;
		while (!Bits.empty())
		{
			if (Bits.front() == 1)
				Q = RMQ[Q][BitCounter];
			Bits.pop();
			BitCounter++;
		}
		cout << Q << '\n';
		NumberOfQueries --;
	}
	return 0;
}