Pagini recente » Cod sursa (job #2840743) | Cod sursa (job #3208477) | Cod sursa (job #2014317) | Cod sursa (job #3130528) | Cod sursa (job #3231569)
#include <cstdint>
#include <fstream>
#define SIZE 250001
int main()
{
std::ifstream fin("stramosi.in");
std::ofstream fout("stramosi.out");
int32_t members, queries;
fin >> members >> queries;
int32_t tree_depth = 0;
int32_t ancestors[SIZE];
for (int32_t i = 1; i <= members; ++i) {
fin >> ancestors[i];
tree_depth += (ancestors[i] == i - 1);
}
if (tree_depth == members) {
while (queries--) {
int32_t x, y;
fin >> x >> y;
fout << (x >= y) ? (x - y) : 0 << '\n';
}
}
else {
while (queries--) {
int32_t x, y;
fin >> x >> y;
while (y-- && x) {
x = ancestors[x];
}
fout << x << '\n';
}
}
return 0;
}