Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <string.h>
- #include <math.h>
- #include <set>
- #include <algorithm>
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define cy cout << "YES\n"
- #define cn cout << "NO\n"
- #define nl "\n"
- #define fi first
- #define se second
- #define MOD 1000000007
- #define inf 2000000000
- #define PI acos(-1)
- #define all(v) v.begin(), v.end()
- constexpr ll LINF = 1LL << 62;
- #define sz(s) s.size()
- #define f0r(j, n) for (ll i = j; i < n; i++)
- #define cin_2d(vec, n, m) \
- for (int i = 0; i < n; i++) \
- for (int j = 0; j < m && cin >> vec[i][j]; j++) \
- ;
- #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0)
- template <typename T = int>
- istream &operator>>(istream &in, vector<T> &v)
- {
- for (auto &x : v)
- in >> x;
- return in;
- }
- template <typename T = int>
- ostream &operator<<(ostream &out, const vector<T> &v)
- {
- for (const T &x : v)
- out << x << " ";
- return out;
- }
- void sherry()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- vector<int> next_element(vector<int> &v)
- {
- int n = v.size();
- vector<int> ans(n, -1);
- stack<int> s;
- for (int i = n - 1; i >= 0; i--)
- {
- while (!s.empty() && v[s.top()] <= v[i])
- s.pop();
- ans[i] = (s.empty() ? -1 : s.top() + 1);
- s.push(i);
- }
- return ans;
- }
- void solve()
- {
- int n;
- cin >> n;
- vector<int> v(n);
- cin >> v;
- vector<int> ans = next_element(v);
- int q;
- cin >> q;
- while (q--)
- {
- int num;
- cin >> num;
- cout << ans[num - 1] << nl;
- }
- }
- int main()
- {
- sherry();
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement