Advertisement
he_obviously

Untitled

Aug 16th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. /*
  4. #include <ext/rope>
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/tree_policy.hpp>
  7. */
  8.  
  9. using namespace std;
  10. /*
  11. using namespace __gnu_pbds;
  12. */
  13.  
  14. /*
  15. #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
  16. */
  17.  
  18. const int MAXN = 100001;
  19. int n;
  20.  
  21. int tree[4 * MAXN], a[MAXN];
  22.  
  23. void read_a() {
  24.     for (int i = 1; i <= n; ++i) {
  25.         cin >> a[i];
  26.     }
  27. }
  28.  
  29. void build(int v, int tl, int tr) {
  30.     if (tr - tl == 1) {
  31.         tree[v] = tl;
  32.         return;
  33.     }
  34.     int tm = (tl + tr) / 2;
  35.     build(2 * v + 1, tl, tm);
  36.     build(2 * v + 2, tm, tr);
  37.     if (a[tree[2 * v + 1]] >= a[tree[2 * v + 2]]) {
  38.         tree[v] = tree[2 * v + 1];
  39.     }
  40.     else {
  41.         tree[v] = tree[2 * v + 2];
  42.     }
  43. }
  44.  
  45. int get_max(int v, int tl, int tr, int l, int r) {
  46.     if (tl >= r || tr <= l) {
  47.         return 0;
  48.     }
  49.     else if (tl >= l && tr <= r) {
  50.         return tree[v];
  51.     }
  52.     else {
  53.         int tm = (tl + tr) / 2;
  54.         int ans1 = get_max(2 * v + 1, tl, tm, l, min(r, tm));
  55.         int ans2 = get_max(2 * v + 2, tm, tr, max(l, tm), r);
  56.         if (a[ans1] >= a[ans2]) {
  57.             return ans1;
  58.         }
  59.         else {
  60.             return ans2;
  61.         }
  62.     }
  63. }
  64.  
  65. void solve() {
  66.     int l, r;
  67.     cin >> l >> r;
  68.     cout << get_max(0, 1, n + 1, l, r + 1) << " ";
  69. }
  70.  
  71. int main() {
  72.  
  73.     ios_base::sync_with_stdio(0);
  74.     cin.tie(0); cout.tie(0);
  75.  
  76.     cin >> n;
  77.  
  78.     read_a();
  79.     a[0] = INT_MIN;
  80.  
  81.     build(0, 1, n + 1);
  82.  
  83.     int k;
  84.     cin >> k;
  85.  
  86.     for (int _ = 0; _ < k; ++_) {
  87.         solve();
  88.     }
  89.  
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement