Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- int n;
- cin >> n;
- vector<int>v(n);
- stack<pair<int,int>>s;
- vector<int>a(n);
- for (int i=0; i<n; i++) {
- cin >> v[i];
- }
- for (int i=n-1; i>=0; i--) {
- if (!s.empty() && s.top().first<v[i]) {
- while (!s.empty() && s.top().first<=v[i]) {
- s.pop();
- }
- if (!s.empty()) a[i]=s.top().second;
- else a[i]=-1;
- s.push({v[i],i+1});
- }
- else {
- if (!s.empty()) a[i]=s.top().second;
- else a[i]=-1;
- s.push({v[i],i+1});
- }
- }
- int q;
- cin >> q;
- while (q--) {
- int x;
- cin >> x;
- cout << a[x-1] << endl;
- }
- return 0;
- }
- /*
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment