RainX_69

Find lexographically largest character in a substring in range [l,r] for q queries| Segment Tree| OA

May 5th, 2023
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/problems/c928f229cc972671d91c5e9f6b222414912cc88a/1
  2.  
  3. Given a string s whose length is n and array queries of length q where each elements of queries is either of type 1 query or type 2 query which is explained ahead.
  4.  
  5. There are two types of query.
  6.  
  7. Query type 1 : ["1",ind,char]  "1" denotes this is type 1 query. In this query you have to change the character at index ind in s to character char.(Data type of ind,char is string in input)
  8.  
  9. Query Type 2: ["2",left,right,k]  "2" denotes this is type 2 query. In this query you have to return kth lexographically largest character  in the subtring of s (it is the kth largest character in sorted order , not the kth distinct character) starting from index left and ending at index right both left and right are inclusive. (Data type of left,right,k is string in input)
  10.  
  11. You have to perform each query in the same order as given in  queries and return an array res such that res array contains the answer for each type2 query in same order as it appeared in queries.
  12.  
  13. Note : 0 based indexing is used.
  14.  
  15. Example 1:
  16.  
  17. Input:
  18. n=4
  19. s="abab"
  20. q=2
  21. queries={{"1","2","d"},{"2","1","3","1"}}
  22. Output:
  23. {"d"}
  24. Explanation:
  25. First query is of type 1 so after changing character at index 2
  26. to d  s becomes abdb . Now Second query is of type 2 in which
  27. the 1st(k=1) lexographically largest character is "d" in substring "bdb"(s[1:3]). So we
  28. returned a array with result of type 2 query {"d"}.
  29. Example 2:
  30.  
  31. Input:
  32. n=3
  33. s="aaa"
  34. q=3
  35. queries={{"1","1","e"},{"1","2","c"},{"2","1","2","2"}}
  36. Output:
  37. {"c"}
  38. Explanation:
  39. After applying first two queries s becomes aec. Now for
  40. the last query which is a type 2 second largest character
  41. in subtring s starting from index 1 to ending at index 2 is "c".
  42. Your Task:
  43. You don't need to read input or print anything. Your task is to complete the function easyTask() which takes an integer n,string s,an integer q and an array queries which contains  queries of type1 and type2  respectively and returns an array res such that res array contains the answer for each type2 query in same order as it appeared in queries.
  44.  
  45. Expected Time Complexity: O(N+(Q*logN))
  46. Expected Space Complexity: O(N)
  47.  
  48.  
  49. Constraints:
  50. 1<=n<=5*10^4
  51. 1<=q<=10^5
  52. 0<=int(left)<=int(right)<=n-1
  53. 0<=int(index)<=n-1
  54. 1<=int(k)<=right-left+1
  55. s and char contains lowercase english letters
  56. The sum of n over all test cases won't exceed 5*10^4.
  57.  
  58. ------------------------------------------------------------------------------------------------------------------------------------
  59.  
  60. class Solution{
  61. public:
  62.     vector<vector<int>> tree;
  63.    
  64.     vector<int> merge(vector<int> &left, vector<int> &right){
  65.         vector<int> freq(26,0);
  66.         for(int i=0;i<26;i++){
  67.             freq[i]=left[i]+right[i];
  68.         }
  69.         return freq;
  70.     }
  71.    
  72.     void buildTree(string &s, int start, int end, int parent){
  73.         if(start==end){
  74.             tree[parent][s[start]-'a']++;
  75.             return;
  76.         }
  77.         int mid=(start+end)/2;
  78.         buildTree(s,start,mid,2*parent+1);
  79.         buildTree(s,mid+1,end,2*parent+2);
  80.         tree[parent]=merge(tree[2*parent+1],tree[2*parent+2]);
  81.         return;
  82.     }
  83.    
  84.     void update(int start, int end, int parent, int index, char c){
  85.         if(start>end){
  86.             return;
  87.         }
  88.         if(start==end){
  89.             vector<int> freq(26,0);
  90.             freq[c-'a']++;
  91.             tree[parent]=freq;
  92.             return;
  93.         }
  94.         int mid=(start+end)/2;
  95.         if(index>mid){
  96.             update(mid+1,end,2*parent+2,index,c);
  97.         }
  98.         else{
  99.             update(start,mid,2*parent+1,index,c);
  100.         }
  101.         tree[parent]=merge(tree[2*parent+1],tree[2*parent+2]);
  102.         return;
  103.     }
  104.    
  105.     vector<int> querySolver(int start, int end, int qstart, int qend, int parent){
  106.         if(qstart>end || qend<start){
  107.             return vector<int>(26,0);
  108.         }
  109.         if(qstart<=start && qend>=end){
  110.             return tree[parent];
  111.         }
  112.         int mid=(start+end)/2;
  113.         auto L=querySolver(start,mid,qstart,qend,2*parent+1);
  114.         auto R=querySolver(mid+1,end,qstart,qend,2*parent+2);
  115.         return merge(L,R);
  116.     }
  117.    
  118.     vector<char> easyTask(int n,string s,int q,vector<vector<string>> &queries){
  119.         tree.resize(4*n+1,vector<int>(26,0));
  120.         buildTree(s,0,n-1,0);
  121.         vector<char> res;
  122.         for(auto query: queries){
  123.             int type=stoi(query[0]);
  124.             if(type==1){
  125.                 int index=stoi(query[1]);
  126.                 char c=query[2][0];
  127.                 update(0,n-1,0,index,c);
  128.             }
  129.             else{
  130.                 int left=stoi(query[1]);
  131.                 int right=stoi(query[2]);
  132.                 int k=stoi(query[3]);
  133.                 auto freq=querySolver(0,n-1,left,right,0);
  134.                 for(int i=25;i>=0;i--){
  135.                     k-=freq[i];
  136.                     if(k<=0){
  137.                         res.push_back(i+'a');
  138.                         break;
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.         return res;
  144.     }
  145. };
Advertisement
Add Comment
Please, Sign In to add comment