lina_os

Untitled

Mar 10th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  8. int n;
  9. cin >> n;
  10. vector<int>v(n);
  11. stack<pair<int,int>>s;
  12. vector<int>a(n);
  13. for (int i=0; i<n; i++) {
  14. cin >> v[i];
  15. }
  16. for (int i=n-1; i>=0; i--) {
  17. if (!s.empty() && s.top().first<v[i]) {
  18. while (!s.empty() && s.top().first<=v[i]) {
  19. s.pop();
  20. }
  21. if (!s.empty()) a[i]=s.top().second;
  22. else a[i]=-1;
  23. s.push({v[i],i+1});
  24. }
  25. else {
  26. if (!s.empty()) a[i]=s.top().second;
  27. else a[i]=-1;
  28. s.push({v[i],i+1});
  29. }
  30.  
  31. }
  32. int q;
  33. cin >> q;
  34. while (q--) {
  35. int x;
  36. cin >> x;
  37. cout << a[x-1] << endl;
  38. }
  39. return 0;
  40. }
  41.  
  42. /*
  43. *
  44. */
Advertisement
Add Comment
Please, Sign In to add comment