Cod sursa(job #2664390)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 28 octombrie 2020 16:17:35
Problema Stramosi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>

using namespace std;

#include <fstream>
//ifstream cin("input.in"); ofstream cout("output.out");
ifstream cin("stramosi.in"); ofstream cout("stramosi.out");

//VARIABLES

const int MAXN = 250005;

int st[MAXN][25];

int LOG[250005];

//FUNCTIONS



//MAIN
int main() {

    int n, m;
    cin >> n >> m;

    for (int i = 2; i <= n; i++) LOG[i] = LOG[i / 2] + 1;

    for (int i = 1; i <= n; i++) cin >> st[i][0];

    for (int p = 1; p <= LOG[n]; p++) {
        for (int i = 1; i <= n; i++) {
            st[i][p] = st[st[i][p - 1]][p - 1];
        }
    }

    for (int i = 1; i <= m; i++) {
        int nod, rang;

        cin >> nod >> rang;

        while (rang) {

            nod = st[nod][LOG[rang]];
            rang -= (1 << LOG[rang]);
        }

        cout << nod << '\n';
    }

    return 0;
}