Cod sursa(job #2649734)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 16 septembrie 2020 08:20:16
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.59 kb

#include <bits/stdc++.h>
 
using namespace std;
 
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
 
int dp[21][250005];
 
int main() {
    int n, m;
    fin >> n >> m;
    for(int i = 1;i <= n; ++i){
        fin >> dp[0][i];
    }
    for(int j = 1;(1 << j) <= n; ++j)
    for(int i = 1;i <= n; ++i)
            dp[j][i] = dp[j-1][dp[j - 1][i]];
    while(m--){
        int q, p;
        fin >> q >> p;
        int ans = q;
        for(int j = 0;j <= 20; ++j){
            if((p & (1 << j)) != 0)
              ans = dp[j][ans];
        }
        fout << ans << "\n";
    }
    return 0;
}