Cod sursa(job #1486489)

Utilizator FairPlay94George Cioroiu FairPlay94 Data 14 septembrie 2015 22:40:21
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    freopen("stramosi.in", "r", stdin);
    freopen("stramosi.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(0);

    vector<int> str[21];
    int n, m;
    cin >> n >> m;
    for (int i = 0; i <= 20; i++) {
        str[i].push_back(0);
    }
    for (int i = 1; i <= n; i++) {
        int aux;
        cin >> aux;
        str[0].push_back(aux);
    }
    for (int j = 1; j <= 20; j++) {
        for (int i = 1; i <= n; i++) {
            str[j].push_back(str[j - 1][str[j - 1][i]]);
        }
    }
    for (int i = 1; i <= m; i++) {
        int q, p, r = 0;
        cin >> q >> p;
        while (q !=0 && p > 0) {
            if (p % 2 == 1) {
                q = str[r][q];
            }
            p >>= 1;
            r++;
        }
        cout << q << "\n";
    }


    return 0;
}