Cod sursa(job #3350169)

Utilizator Commander_XDunel Stefan-Octavian Commander_X Data 6 aprilie 2026 09:07:42
Problema Stramosi Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#include <vector>

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

using namespace std;

vector<vector<int>> T;
int N,Q;

int main()
{
    fin >> N >> Q;

    int x,p;

    T.resize(N+1);
    for(int i = 1; i <= N; ++i)
    {
        fin >> x;
        if(x == 0)
            T[i].push_back(0);
        else
        {
            T[i] = T[x];
            T[i].push_back(x);
        }
    }

    while(Q--)
    {
        fin >> x >> p;
        if(p <= T[x].size())
            fout << T[x][T[x].size() - p] << '\n';
        else
            fout << 0 << '\n';
    }
}