Advertisement
Rishav_hitk_cse

power of three

Apr 6th, 2021
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. /*
  2. Power of 3
  3.  
  4. Problem Description
  5.  
  6. Given a binary string A of size N and an integer matrix B of size Q x 3.
  7.  
  8. Matrix B has Q queries:
  9.  
  10.     For queries of type B[i][0] = 1, flip the value at index B[i][1] in A if and only if the value at that index is 0 and return -1.
  11.     For queries of type B[i][0] = 0, Return the value of the binary string from index B[i][1] to B[i][2] modulo 3.
  12.  
  13.     Note: Rows are numbered from top to bottom and columns are numbered from left to right.
  14.  
  15.  
  16.  
  17. Problem Constraints
  18.  
  19. 1 <= N <= 100000
  20. 1 <= Q <= 200000
  21. 1 <= B[i][1], B[i][2] <= N
  22. B[i][1] <= B[i][2]
  23.  
  24.  
  25. Input Format
  26.  
  27. The first argument given is the string A.
  28. The second argument given is the integer matrix B of size Q * 3.
  29.  
  30.  
  31. Output Format
  32.  
  33. Return an array of size Q where ith value is answer to ith query.
  34.  
  35.  
  36. Example Input
  37.  
  38. Input 1:
  39.  
  40.  A = 10010
  41.  B = [  [0, 3, 5]
  42.         [0, 3, 4]
  43.         [1, 2, -1]
  44.         [0, 1, 5]
  45.      ]
  46.  
  47. Input 2:
  48.  
  49.  A = 11111
  50.  B = [
  51.         [0, 2, 4]
  52.         [1, 2, -1
  53.         [0, 2, 4]]
  54.      ]
  55.  
  56.  
  57.  
  58. Example Output
  59.  
  60. Output 1:
  61.  
  62.  [2, 1, -1, 2]
  63.  
  64. Output 2:
  65.  
  66.  [1, -1, 1]
  67.  
  68.  
  69.  
  70. Example Explanation
  71.  
  72. Explanation 1:
  73.  
  74.  For query 1, binary string from index 3 to 5 is 010 = 2. So 2 mod 3 = 2.
  75.  For query 2, binary string from index 3 to 4 is 01 = 1. So 1 mod 3 = 1.
  76.  After query 3, given string changes to 11010.
  77.  For query 4, binary string from index 1 to 5 is 11010 = 26. So 26 mod 3 = 2.
  78.  So, output array is [2, 1, -1, 2].
  79.  
  80. Explanation 2:
  81.  
  82.  For query 1, binary string from index 2 to 4 is 111 = 7. So 7 od 3 = 1.
  83.  After query 2, string remains same as there is already 1 at index 2.
  84.  For query 3, binary string from index 2 to 4 is 111 = 7. So 7 od 3 = 1.
  85.  So, output array is [1, -1, 1].
  86.  */
  87.  
  88.     const int mod = 3;
  89.     vector<int> tree;
  90.     void build(vector<int> &a,int v,int l,int r){
  91.         if(l==r)
  92.             tree[v] = a[l];
  93.         else{
  94.             int mid = (l+r)/2;
  95.             build(a,2*v,l,mid);
  96.             build(a,2*v+1,mid+1,r);
  97.             tree[v] = (tree[2*v]+tree[2*v+1])%3;
  98.         }
  99.     }
  100.     int range_sum(int v,int tl,int tr,int l,int r){
  101.         if(l>r)
  102.             return 0;
  103.         if(l==tl and r==tr)
  104.             return tree[v];
  105.         int tm = (tl+tr)/2;
  106.         return (range_sum(2*v,tl,tm,l,min(tm,r))+range_sum(2*v+1,tm+1,tr,max(l,tm+1),r))%3;
  107.     }
  108.     void update(int v,int tl,int tr,int idx,int val){
  109.         if(tl == tr)
  110.             tree[v] = val;
  111.         else{
  112.             int tm = (tl+tr)/2;
  113.             if(idx<=tm)
  114.                 update(2*v,tl,tm,idx,val);
  115.             else
  116.                 update(2*v+1,tm+1,tr,idx,val);
  117.             tree[v] = (tree[2*v]+tree[2*v+1])%3;
  118.         }
  119.     }
  120.     vector<int> Solution::solve(string s, vector<vector<int> > &queries) {
  121.         const int n = s.size();
  122.         tree.assign(4*n,0);
  123.         vector<int> power(n);
  124.         vector<int> a(n);
  125.         power[0] = 1;
  126.         for(int i=1;i<n;i++){
  127.             power[i] = (power[i-1]*2)%mod;
  128.         }
  129.         reverse(power.begin(),power.end());
  130.         for(int i=0;i<n;i++){
  131.             if(s[i]=='1')
  132.                 a[i] = power[i];
  133.         }
  134.        
  135.         build(a,1,0,n-1);
  136.         vector<int> ans;
  137.         for(auto &q:queries){
  138.             int x = q[0],u = q[1]-1,v = q[2]-1;
  139.             if(x){
  140.                 if(s[u]=='0'){
  141.                     s[u] = '1';
  142.                     a[u] = power[u];
  143.                     update(1,0,n-1,u,power[u]);
  144.                 }
  145.                 ans.push_back(-1);
  146.             }else{
  147.                 int curr = range_sum(1,0,n-1,u,v);
  148.                 ans.push_back((curr*power[v])%3);
  149.             }
  150.         }
  151.         return ans;
  152.     }
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement