#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 4000005
#define ff first
#define ss second
#define pb push_back
#define sz(s) (int)s.size()
#define rep(c, a, b) for(c = a; c <= b; c++)
int n, t, q, m;
vector <int> v[N];
deque <int> ans;
void solve(int x) {
vector <int> s;
for(auto i : v[x]) {
s.pb(i);
}
sort(s.begin(),s.end());
for(auto i : s) {
solve(i);
}
ans.pb(x);
return;
}
int main () {
ios::sync_with_stdio(0);cin.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++) {
int x;
cin >> x;
if(x)v[x].pb(i);
else t = i;
}
solve(t);
for(int i = 1; i <= q; i++) {
int x;
cin >> t >> x;
while(t != x) {
ans.pop_front();
t++;
}
cout << ans[0] << '\n';
ans.pop_front();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |