Cod sursa(job #1644683)

Utilizator mariapascuMaria Pascu mariapascu Data 10 martie 2016 08:14:13
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>
using namespace std;

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

const int MaxL = 20, MaxN = 250004;
int N, M;
int dp[MaxL][MaxN];    //d[i][j] - al (1 << i) - lea stramos al lui j

int main() {
    fin >> N >> M;
    for (int j = 1; j <= N; j++)
        fin >> dp[0][j];
    for (int i = 1; (1 << i) <= N; i++)
        for (int j = 1; j <= N; j++)
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
    for (int i = 0, x, y; i < M; i++) {
        fin >> x >> y;
        for (int j = 0; (1 << j) <= y; j++)
            if ((1 << j) & y)
                x = dp[j][x];
        fout << x << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}