RainX_69

LAZY PROPAGATION QUERIES PROBLEM | HARD | MUST DO

Feb 19th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | Source Code | 0 0
  1. https://leetcode.com/problems/handling-sum-queries-after-update/
  2.  
  3. You are given two 0-indexed arrays nums1 and nums2 and a 2D array queries of queries. There are three types of queries:
  4.  
  5. For a query of type 1, queries[i] = [1, l, r]. Flip the values from 0 to 1 and from 1 to 0 in nums1 from index l to index r. Both l and r are 0-indexed.
  6. For a query of type 2, queries[i] = [2, p, 0]. For every index 0 <= i < n, set nums2[i] = nums2[i] + nums1[i] * p.
  7. For a query of type 3, queries[i] = [3, 0, 0]. Find the sum of the elements in nums2.
  8. Return an array containing all the answers to the third type queries.
  9.  
  10.  
  11.  
  12. Example 1:
  13.  
  14. Input: nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]
  15. Output: [3]
  16. Explanation: After the first query nums1 becomes [1,1,1]. After the second query, nums2 becomes [1,1,1], so the answer to the third query is 3. Thus, [3] is returned.
  17. Example 2:
  18.  
  19. Input: nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]
  20. Output: [5]
  21. Explanation: After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned.
  22.  
  23.  
  24. Constraints:
  25.  
  26. 1 <= nums1.length,nums2.length <= 10^5
  27. nums1.length = nums2.length
  28. 1 <= queries.length <= 10^5
  29. queries[i].length = 3
  30. 0 <= l <= r <= nums1.length - 1
  31. 0 <= p <= 106
  32. 0 <= nums1[i] <= 1
  33. 0 <= nums2[i] <= 10^9
  34. ---------------------------------------------------------------------------------------------------------------------------------------
  35.  
  36. class Solution {
  37. private:
  38.     vector<long long> tree;
  39.     vector<bool> lazy;
  40. public:
  41.     void build(vector<int> &bits, long long start, long long end, long long parent){
  42.         if(start==end){
  43.             tree[parent]=bits[start];
  44.             return;
  45.         }
  46.         long long mid=(start+end)/2;
  47.         build(bits,start,mid,2*parent+1);
  48.         build(bits,mid+1,end,2*parent+2);
  49.         tree[parent]=tree[2*parent+1]+tree[2*parent+2];
  50.     }
  51.    
  52.     void querySolver(long long start, long long end, long long qstart, long long qend, long long parent){
  53.         if(start>end){
  54.             return;
  55.         }
  56.  
  57.         if(lazy[parent]==true){ // lazy happened
  58.             long long totalBits=end-start+1;
  59.             tree[parent]=totalBits-tree[parent];
  60.             if(start!=end){
  61.                 lazy[2*parent+1]=!lazy[2*parent+1];
  62.                 lazy[2*parent+2]=!lazy[2*parent+2];
  63.             }
  64.             lazy[parent]=false;
  65.         }
  66.        
  67.         if(qstart>end || qend<start){ // no overlapping
  68.             return;
  69.         }
  70.        
  71.         if(qstart<=start && qend>=end){  // lazy work
  72.             long long totalBits=end-start+1;
  73.             tree[parent]=totalBits-tree[parent];
  74.             if(start!=end){
  75.                 lazy[2*parent+1]=!lazy[2*parent+1];
  76.                 lazy[2*parent+2]=!lazy[2*parent+2];
  77.             }
  78.             return;
  79.         }
  80.        
  81.         long long mid=(start+end)/2;
  82.         querySolver(start,mid,qstart,qend,2*parent+1);
  83.         querySolver(mid+1,end,qstart,qend,2*parent+2);
  84.         tree[parent]=tree[2*parent+1]+tree[2*parent+2];
  85.     }
  86.    
  87.     vector<long long> handleQuery(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {
  88.         /*
  89.         The second query says:-
  90.         nums2[i]=nums2[i]+nums1[i]*p
  91.        
  92.         Elaborating...
  93.                 (nums2[0] + nums1[0]*p) + (nums2[1] + nums1[1]*p) + ........
  94.                 Collecting nums2 and nums1 together..
  95.                 (nums2[0]+nums2[1]+...) + (nums1[0]+nums1[1]+...) *p
  96.                 summation(nums2) + summation(nums1)*p
  97.                
  98.                 So the answer for queries can be given if you can efficiently count the sum of
  99.                 nums1 after each update (l,r). nums2 sum is just updated by the new sum.
  100.         */
  101.         long long n=nums1.size();
  102.         lazy.resize(4*n+1);
  103.         tree.resize(4*n+1,false);
  104.  
  105.         build(nums1,0,n-1,0);
  106.        
  107.         long long sum_nums2=accumulate(nums2.begin(),nums2.end(),0LL);
  108.        
  109.         vector<long long> res;
  110.        
  111.         for(auto query: queries){
  112.             long long type=query[0];
  113.             if(type==1){
  114.                 long long l=query[1];
  115.                 long long r=query[2];
  116.                 querySolver(0,n-1,l,r,0);              
  117.             }
  118.             if(type==2){
  119.                 long long p=query[1];
  120.                 long long newSum=sum_nums2+tree[0]*p;
  121.                 sum_nums2=newSum;
  122.             }
  123.             if(type==3){
  124.                 res.push_back(sum_nums2);
  125.             }
  126.         }
  127.         return res;
  128.     }
  129.    
  130. };
Advertisement
Add Comment
Please, Sign In to add comment