Cod sursa(job #3233811)

Utilizator prares06Papacioc Rares-Ioan prares06 Data 5 iunie 2024 00:11:45
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.58 kb
#include<bits/stdc++.h>

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

const int MAX = 2.5e5 + 5;
const int LOG_MAX = 18;
int n, m;
int st[LOG_MAX][MAX];

int main(){
    fin >> n >> m;
    for(int i = 1; i <= n; ++i)
        fin >> st[0][i];
    for(int i = 1; (1 << i) <= n; ++i)
    for(int j = 1; j <= n; ++j)
        st[i][j] = st[i - 1][st[i - 1][j]];
    int p, q;
    for(;m--;){
        fin >> q >> p;
        for(int i = 0; (1 << i) <= p; ++i)
            if(p & (1 << i))
                q = st[i][q];
        fout << q << '\n';
    }
}