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