Advertisement
ekzolot

Untitled

Apr 27th, 2024
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void build(vector<pair<int, int>>& tree, int idx, int l, int r, vector<int>& a){
  4.     if (r-l==1){
  5.         tree[idx]={a[l], l};
  6.         return;
  7.     }
  8.     int m=(l+r)/2;
  9.     build(tree, 2*idx+1, l, m, a);
  10.     build(tree, 2*idx+2, m, r, a);
  11.     tree[idx]=max(tree[2*idx+1], tree[2*idx+2]);
  12. }
  13. void change(vector<pair<int, int>>& tree, int l, int r, int idx, int i, int x){
  14.     if (i<l || i>=r){
  15.         return;
  16.     }
  17.     if (r-l==1){
  18.         tree[idx]={x, i};
  19.         return;
  20.     }
  21.     int m=(l+r)/2;
  22.     change(tree, l, m, 2*idx+1, i, x);
  23.     change(tree, m, r, 2*idx+2, i, x);
  24.     tree[idx]=max(tree[2*idx+1], tree[2*idx+2]);
  25. }
  26. pair<int, int> ans(vector<pair<int, int>>& tree, int idx, int l, int r, int L, int R){
  27.     if (R<=l || L>=r){
  28.         return {-1, -1};
  29.     }
  30.     if (l>=L && r<=R){
  31.         return tree[idx];
  32.     }
  33.     int m=(l+r)/2;
  34.     return max(ans(tree, 2*idx+1, l, m, L, R), ans(tree, 2*idx+2, m, r, L, R));
  35. }
  36. int main(){
  37.     int n;
  38.     cin>>n;
  39.     vector<int> a(n);
  40.     for (int i=0; i<n; i++){
  41.         cin>>a[i];
  42.     }
  43.     int k;
  44.     cin>>k;
  45.     vector<pair<int, int>> tree(4*n);
  46.     build(tree, 0, 0, n, a);
  47.     while(k--){
  48.         int l, r;
  49.         cin>>l>>r;
  50.         l--;
  51.         cout<<ans(tree, 0, 0, n, l, r).first<<" "<<ans(tree, 0, 0, n, l, r).second+1<<"\n";
  52.     }
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement