Cod sursa(job #1559711)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 31 decembrie 2015 14:45:24
Problema Stramosi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 250005;
const int BMax = 1e5 + 5;

int v[NMax];

int pos = BMax - 1;
char Buffer[BMax];
inline void Read(int &x){
    while(!isdigit(Buffer[pos])){
        if(++pos == BMax){
            fin.read(Buffer, BMax);
            pos = 0;
        }
    }
    x = 0;
    while(isdigit(Buffer[pos])){
        x = x * 10 + (Buffer[pos] - '0');
        if(++pos == BMax){
            fin.read(Buffer, BMax);
            pos = 0;
        }
    }
}

int main(){
    int n, t, q, p;
    Read(n); Read(t);
    for(int i = 1; i <= n; i++) Read(v[i]);
    while(t--){
        Read(q); Read(p);
        while(p > 0 && q != 0){
            q = v[q];
            p--;
        }
        fout << q << "\n";
    }
    return 0;
}