RainX_69

Queries on subarray INCREASING/DECREASING | MUST DO | HARD | SEGMENT TREE | OA

Mar 9th, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/problems/akku-and-arrays4452/1?page=1&difficulty[]=1&difficulty[]=2&status[]=unsolved&category[]=Trie&category[]=Segment-Tree&sortBy=submissions
  2.  
  3. Akku have solved many problems, she is genius. One day her friend gave her an Array of size n and asked her to perform
  4. some queries of following type:
  5. Each query consists of three integers
  6. 1 A B : Update the Array at index A by value B
  7. 2 A B : if the subarray from index A to B (both inclusive) is
  8.         1. Both increasing(Non-decreasing) and decreasing(Non-increasing) print -1
  9.         2. Only increasing(Non-decreasing) print 0
  10.         3. Only decreasing(Non-increasing) print 1
  11.         4. Neither increasing nor decreasing print -1
  12.  
  13. Akku needs your help, can you help her.
  14.  
  15. Example 1:
  16.  
  17. Input: nums = {1,5,7,4,3,5,9},
  18. Queries = {{2,1,3},{1,7,4},{2,6,7}}
  19. Output: {0,1}
  20.  
  21. Explanation: For the 1st query given :
  22. A = 1, B = 3. From 1 to 3(1,5,7) elements
  23. are in increasing order. So answer is 0.
  24. For the 2nd query we have to update the 7th
  25. element of the array by 4. So new updated array
  26. will be {1,5,7,4,3,5,4}
  27. For the 3rd query A = 6, B = 7. From 6 to 7
  28. (5, 4) elements are in descending order. So
  29. answer is 1.
  30.  
  31. ---------------------------------------------------------------------------------------------------------------------------------------
  32.  
  33. struct Node{
  34.     bool incs=false;
  35.     bool decs=false;
  36.     int L=-1;  // the start value of sequence
  37.     int R=-1;  // the end value of sequence
  38. };
  39.  
  40. class Solution {
  41. private:
  42.   vector<Node> tree;
  43. public:
  44.   Node merger(Node &n1, Node &n2){
  45.       if(n1.L==-1 || n2.L==-1){
  46.           return n1.L==-1 ? n2 : n1;
  47.       }
  48.       Node res;
  49.       res.L=n1.L;
  50.       res.R=n2.R;
  51.       if(n1.incs==true && n2.incs==true && n1.R<=n2.L){  
  52.           res.incs=true;
  53.       }
  54.       if(n1.decs==true && n2.decs==true && n1.R>=n2.L){
  55.           res.decs=true;
  56.       }
  57.       return res;
  58.   }
  59.  
  60.   void build(vector<int> &nums, int start, int end, int parent){
  61.       if(start==end){
  62.           tree[parent]={true,true,nums[start],nums[start]};
  63.           return;
  64.       }
  65.       int mid=(start+end)/2;
  66.       build(nums,start,mid,2*parent+1);
  67.       build(nums,mid+1,end,2*parent+2);
  68.       tree[parent]=merger(tree[2*parent+1],tree[2*parent+2]);
  69.   }
  70.  
  71.   void update(int start, int end, int parent, int index, int val){
  72.       if(start==end){
  73.           tree[parent]={true,true,val,val};
  74.           return;
  75.       }
  76.       int mid=(start+end)/2;
  77.       if(index>mid){
  78.         update(mid+1,end,2*parent+2,index,val);
  79.       }
  80.       else{
  81.           update(start,mid,2*parent+1,index,val);
  82.       }
  83.       tree[parent]=merger(tree[2*parent+1],tree[2*parent+2]);
  84.   }
  85.  
  86.   Node query(int start, int end, int parent, int qstart, int qend){
  87.       if(qend<start || qstart>end){
  88.           return {false,false,-1,-1};
  89.       }
  90.       if(qend>=end && qstart<=start){
  91.           return tree[parent];
  92.       }
  93.       int mid=(start+end)/2;
  94.       auto L=query(start,mid,2*parent+1,qstart,qend);
  95.       auto R=query(mid+1,end,2*parent+2,qstart,qend);
  96.       return merger(L,R);
  97.   }
  98.  
  99.   vector<int>solveQueries(vector<int>nums, vector<vector<int>>Queries){
  100.       int n=nums.size();
  101.       tree.resize(4*n+1);
  102.       build(nums,0,n-1,0);
  103.      
  104.       vector<int> res;
  105.      
  106.       for(auto q: Queries){
  107.           int type=q[0];
  108.           if(type==1){
  109.               update(0,n-1,0,q[1]-1,q[2]);
  110.           }
  111.           else{
  112.               Node ans=query(0,n-1,0,q[1]-1,q[2]-1);
  113.               if(ans.incs==ans.decs){  // both true or false
  114.                 res.push_back(-1);
  115.               }
  116.               else if(ans.incs==true){
  117.                 res.push_back(0);
  118.               }
  119.               else if(ans.decs==true){
  120.                 res.push_back(1);
  121.               }
  122.           }
  123.       }
  124.       return res;
  125.   }
  126. };
Advertisement
Add Comment
Please, Sign In to add comment