Cod sursa(job #2664395)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 28 octombrie 2020 16:21:23
Problema Stramosi Scor 100
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[25][MAXN];

int LOG[MAXN];

//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[0][i];

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

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

        cin >> nod >> rang;

        while (rang) {

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

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

    return 0;
}