Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- /*
- #include <ext/rope>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- */
- using namespace std;
- /*
- using namespace __gnu_pbds;
- */
- /*
- #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
- */
- const int MAXN = 100001;
- int n;
- int tree[4 * MAXN], a[MAXN];
- void read_a() {
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- }
- void build(int v, int tl, int tr) {
- if (tr - tl == 1) {
- tree[v] = tl;
- return;
- }
- int tm = (tl + tr) / 2;
- build(2 * v + 1, tl, tm);
- build(2 * v + 2, tm, tr);
- if (a[tree[2 * v + 1]] >= a[tree[2 * v + 2]]) {
- tree[v] = tree[2 * v + 1];
- }
- else {
- tree[v] = tree[2 * v + 2];
- }
- }
- int get_max(int v, int tl, int tr, int l, int r) {
- if (tl >= r || tr <= l) {
- return 0;
- }
- else if (tl >= l && tr <= r) {
- return tree[v];
- }
- else {
- int tm = (tl + tr) / 2;
- int ans1 = get_max(2 * v + 1, tl, tm, l, min(r, tm));
- int ans2 = get_max(2 * v + 2, tm, tr, max(l, tm), r);
- if (a[ans1] >= a[ans2]) {
- return ans1;
- }
- else {
- return ans2;
- }
- }
- }
- void solve() {
- int l, r;
- cin >> l >> r;
- cout << get_max(0, 1, n + 1, l, r + 1) << " ";
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- cin >> n;
- read_a();
- a[0] = INT_MIN;
- build(0, 1, n + 1);
- int k;
- cin >> k;
- for (int _ = 0; _ < k; ++_) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement