Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6.     int mn,mx;
  7. };
  8.  
  9. node combine(node n1, node n2){
  10.     node ans;
  11.     ans.mn=min(n1.mn,n2.mn);
  12.     ans.mx=max(n1.mx,n2.mx);
  13.     return ans;
  14. }
  15.  
  16. node build_from_one(int n){
  17.     node ans;
  18.     ans.mx=n;
  19.     ans.mn=n;
  20.     return ans;
  21. }
  22.  
  23. node shitty_node;
  24. vector<int> a;
  25. vector<node> t;
  26.  
  27. void build(int v, int tl, int tr){
  28.     if(tl==tr){
  29.         t[v]=build_from_one(a[tl]);
  30.         return;
  31.     }
  32.     int tm=(tl+tr)/2;
  33.     build(v*2, tl, tm);
  34.     build(v*2+1,tm+1,tr);
  35.     t[v]=combine(t[v*2],t[v*2+1]);
  36. }
  37.  
  38. node get_ans(int v, int tl, int tr, int r, int l){
  39.     if(l>r){
  40.         return shitty_node;
  41.     }
  42.     if(tl==r and tr==l)
  43.         return t[v];
  44.     int tm=(tl+tr)/2;
  45.     return combine(get_ans(v*2,tl,tm,min(r,tm),l), get_ans(v*2+1,tm+1,tr,r,max(l,tm+1)));
  46. }
  47.  
  48. int main(){
  49.     shitty_node.mn=1e9;
  50.     shitty_node.mx=-1e9;
  51.     int n;
  52.     cin>>n;
  53.     a.resize(n);t.resize(n*4,shitty_node);
  54.     for(int i=0;i<n;i++)
  55.         cin>>a[i];
  56.     build(0,0,n-1);
  57.     int q;cin>>q;
  58.     while(q--){
  59.         int ll,rr;
  60.         cin>>ll>>rr;ll--;rr--;
  61.         node ans=get_ans(0,0,n-1,rr,ll);
  62.         cout<<ans.mn<<' '<<ans.mx<<'\n';
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement