Cod sursa(job #2364773)

Utilizator FrequeAlex Iordachescu Freque Data 4 martie 2019 10:48:47
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define enter cout << '\n'

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef vector <ll> vl;
typedef vector <pll> vll;
typedef queue <int> qi;
typedef queue <pii> qii;
typedef queue <ll> ql;
typedef queue <pll> qll;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 9;
const double EPSILON = 1e-10;
const int NMAX = 2.5e5 + 5;
const int LMAX = 22;

ifstream fin("stramosi.in");
ofstream fout("stramosi.out");

int n, m;
int stramosi[LMAX][NMAX];

void init()
{
    for (int i = 1; i < LMAX; ++i)
        for (int j = 1; j <= n; ++j)
            stramosi[i][j] = stramosi[i - 1][stramosi[i - 1][j]];
}

int query(int q, int p)
{
    int i = 0;

    while (p)
    {
        if (p & 1) q = stramosi[i][q];
        p >>= 1;
        ++i;
    }

    return q;
}

void read()
{
    fin >> n >> m;
    for (int i = 1; i <= n; ++i)
        fin >> stramosi[0][i];
}

int main()
{
    read();
    init();

    while (m--)
    {
        int q, p;
        fin >> q >> p;
        fout << query(q, p) << '\n';
    }

    return 0;
}