Cod sursa(job #2935333)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 6 noiembrie 2022 15:45:36
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = uint64_t;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpl = vector<pl>;
using vti = vector<ti>;
using vtl = vector<tl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvpil = vector<vpil>;
using vvpli = vector<vpli>;
using vvpl = vector<vpl>;
using vvti = vector<vti>;
using vvtl = vector<vtl>;


#if 1
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
#define cin fin
#define cout fout
#endif

int n, q;

const int nmx = 25e4;
int a[20][nmx];
int t[nmx];
int lg[nmx];
void prec()
{
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i / 2] + 1;

	for (int i = 1; i <= n; i++)
		a[0][i] = t[i];

	for (int i = 1; (1 << i) <= n; i++)
		for (int j = 1; j <= n; j++)
			a[i][j] = a[i - 1][a[i - 1][j]];
}

int query(int nd, int nr)
{
	while (nr)
	{
		int k = lg[nr];
		nd = a[k][nd];
		nr -= (1 << k);
	}
	return nd;
}

int main()
{
	cin >> n >> q;
	for (int i = 1; i <= n; i++)
		cin >> t[i];

	prec();

	while (q--)
	{
		int u, nr;
		cin >> u >> nr;
		cout << query(u, nr) << "\n";
	}

}