fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. // -------- Monotonic Stack -----------
  7.  
  8. int main() {
  9. ios_base::sync_with_stdio(false);
  10. cin.tie(NULL);
  11.  
  12. ll n; cin >> n;
  13. vector<ll> v(n), res(n, -1);
  14. stack<ll> st;
  15.  
  16. for(auto &x: v) cin >> x;
  17.  
  18. for(int i = 0; i < n; i++){
  19. while (!st.empty() && v[st.top()] < v[i]){
  20. res[st.top()] = i+1;
  21. st.pop();
  22. }
  23. st.push(i);
  24. }
  25.  
  26. ll q, x; cin >> q;
  27. while (q--)
  28. {
  29. cin >> x;
  30. cout << res[x-1] << "\n";
  31. }
  32.  
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
46 82 28 62 92
5
1
5
3
4
2
stdout
2
-1
4
5
5