Cod sursa(job #2679433)

Utilizator alexradu04Radu Alexandru alexradu04 Data 30 noiembrie 2020 16:07:53
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

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

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 250005;

int dad[18][N];

int main()
{
    usain_bolt();

    int n, q;

    fin >> n >> q;
    for(int i = 1; i <= n; ++i) {
        fin >> dad[0][i];
    }
    for(int j = 1; j <= 17; ++j) {
        for(int i = 1; i <= n; ++i) {
            dad[j][i] = dad[j-1][dad[j-1][i]];
        }
    }
    while(q--) {
        int x, y;

        fin >> x >> y;

        int cnt = 0;

        while(y > 0) {
            if(y & 1) {
                x = dad[cnt][x];
            }
            y >>= 1, ++cnt;
        }
        fout << x << "\n";
    }
    return 0;
}