Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://practice.geeksforgeeks.org/problems/akku-and-arrays4452/1?page=1&difficulty[]=1&difficulty[]=2&status[]=unsolved&category[]=Trie&category[]=Segment-Tree&sortBy=submissions
- Akku have solved many problems, she is genius. One day her friend gave her an Array of size n and asked her to perform
- some queries of following type:
- Each query consists of three integers
- 1 A B : Update the Array at index A by value B
- 2 A B : if the subarray from index A to B (both inclusive) is
- 1. Both increasing(Non-decreasing) and decreasing(Non-increasing) print -1
- 2. Only increasing(Non-decreasing) print 0
- 3. Only decreasing(Non-increasing) print 1
- 4. Neither increasing nor decreasing print -1
- Akku needs your help, can you help her.
- Example 1:
- Input: nums = {1,5,7,4,3,5,9},
- Queries = {{2,1,3},{1,7,4},{2,6,7}}
- Output: {0,1}
- Explanation: For the 1st query given :
- A = 1, B = 3. From 1 to 3(1,5,7) elements
- are in increasing order. So answer is 0.
- For the 2nd query we have to update the 7th
- element of the array by 4. So new updated array
- will be {1,5,7,4,3,5,4}
- For the 3rd query A = 6, B = 7. From 6 to 7
- (5, 4) elements are in descending order. So
- answer is 1.
- ---------------------------------------------------------------------------------------------------------------------------------------
- struct Node{
- bool incs=false;
- bool decs=false;
- int L=-1; // the start value of sequence
- int R=-1; // the end value of sequence
- };
- class Solution {
- private:
- vector<Node> tree;
- public:
- Node merger(Node &n1, Node &n2){
- if(n1.L==-1 || n2.L==-1){
- return n1.L==-1 ? n2 : n1;
- }
- Node res;
- res.L=n1.L;
- res.R=n2.R;
- if(n1.incs==true && n2.incs==true && n1.R<=n2.L){
- res.incs=true;
- }
- if(n1.decs==true && n2.decs==true && n1.R>=n2.L){
- res.decs=true;
- }
- return res;
- }
- void build(vector<int> &nums, int start, int end, int parent){
- if(start==end){
- tree[parent]={true,true,nums[start],nums[start]};
- return;
- }
- int mid=(start+end)/2;
- build(nums,start,mid,2*parent+1);
- build(nums,mid+1,end,2*parent+2);
- tree[parent]=merger(tree[2*parent+1],tree[2*parent+2]);
- }
- void update(int start, int end, int parent, int index, int val){
- if(start==end){
- tree[parent]={true,true,val,val};
- return;
- }
- int mid=(start+end)/2;
- if(index>mid){
- update(mid+1,end,2*parent+2,index,val);
- }
- else{
- update(start,mid,2*parent+1,index,val);
- }
- tree[parent]=merger(tree[2*parent+1],tree[2*parent+2]);
- }
- Node query(int start, int end, int parent, int qstart, int qend){
- if(qend<start || qstart>end){
- return {false,false,-1,-1};
- }
- if(qend>=end && qstart<=start){
- return tree[parent];
- }
- int mid=(start+end)/2;
- auto L=query(start,mid,2*parent+1,qstart,qend);
- auto R=query(mid+1,end,2*parent+2,qstart,qend);
- return merger(L,R);
- }
- vector<int>solveQueries(vector<int>nums, vector<vector<int>>Queries){
- int n=nums.size();
- tree.resize(4*n+1);
- build(nums,0,n-1,0);
- vector<int> res;
- for(auto q: Queries){
- int type=q[0];
- if(type==1){
- update(0,n-1,0,q[1]-1,q[2]);
- }
- else{
- Node ans=query(0,n-1,0,q[1]-1,q[2]-1);
- if(ans.incs==ans.decs){ // both true or false
- res.push_back(-1);
- }
- else if(ans.incs==true){
- res.push_back(0);
- }
- else if(ans.decs==true){
- res.push_back(1);
- }
- }
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment