Cod sursa(job #2923329)

Utilizator tescovschimarioTescovschi Mario tescovschimario Data 12 septembrie 2022 19:41:00
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
using namespace std;
ifstream cin ("stramosi.in");
ofstream cout ("stramosi.out");
int dp[250002][20], n, parent[350002], LOG, m, x, y;

void bl(){
    for(int i = 1; i <= n; i++){
        dp[i][0] = parent[i];
        for(int j = 1; j < LOG; j++)
            dp[i][j] = dp[ dp[i][j-1] ][j-1];
    }
}

void yes(int x, int y){
    for(int j = LOG - 1; j >= 0; j--)
        if(y >= (1 << j)){
            x = dp[x][j];
            y -= 1 << j;}
    cout << x << '\n';
}

int main() {

    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> parent[i];
    while ((1 << LOG) <= n) LOG++;
    bl();

  /*  for(int i = 0; i < n; i++, cout << '\n')
        for(int j = 0; j < LOG; j++)
            cout << dp[i][j] << " ";
    cout << endl;
*/
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        yes(x, y);
 }

}