Advertisement
Spyder_ab

Xor-Key-Hackerrank_Solution

Oct 19th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. int val[16];
  6. class node{
  7. public:
  8. node* left;node* right;
  9. vector<int>s;
  10. node(node* l,node* r){
  11. left=l;right=r;
  12. }
  13. };
  14. class Trie{
  15. public:
  16. node* root=NULL;
  17.  
  18. static void insert(node** temp,string &s,int pos,int k){
  19. if(pos==s.size()){
  20. (*temp)->s.push_back(k);
  21. return;
  22. }
  23. if(pos!=0)
  24. (*temp)->s.push_back(k);
  25. if(s[pos]=='0'){
  26. if((*temp)->left==NULL){
  27. (*temp)->left=new node(NULL,NULL);
  28. }
  29. insert(&((*temp)->left),s,pos+1,k);
  30.  
  31. }else{
  32. if((*temp)->right==NULL){
  33. (*temp)->right=new node(NULL,NULL);
  34. }
  35. insert(&((*temp)->right),s,pos+1,k);
  36. }
  37. }
  38. void TrieInsert(string &s,int k){
  39. if(root==NULL){
  40. root=new node(NULL,NULL);
  41. }
  42. insert(&root,s,0,k);
  43. }
  44. bool isValid(node** temp,int l,int r){
  45. auto z=lower_bound((*temp)->s.begin(),(*temp)->s.end(),l);
  46. if(z!=(*temp)->s.end()){
  47. int x=*z;
  48. if(x<=r){
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. int TrieMaxi(node** temp,string &s,int pos,int l,int r){
  55. if(pos==s.size()){
  56. return 0;
  57. }
  58. if(s[pos]=='0'){
  59. if((*temp)->right!=NULL&&isValid(&((*temp)->right),l,r)){
  60. return val[s.size()-1-pos]+TrieMaxi(&((*temp)->right),s,pos+1,l,r);
  61. }
  62. return TrieMaxi(&((*temp)->left),s,pos+1,l,r);
  63.  
  64. }
  65. if((*temp)->left!=NULL&&isValid(&((*temp)->left),l,r)){
  66. return val[s.size()-1-pos]+TrieMaxi(&((*temp)->left),s,pos+1,l,r);
  67. }
  68. return TrieMaxi(&((*temp)->right),s,pos+1,l,r);
  69. }
  70.  
  71. int getMyMaxi(string &s,int l,int r){
  72. return TrieMaxi(&root,s,0,l,r);
  73. }
  74. };
  75. int32_t main(){
  76.  
  77. IOS
  78. val[0]=1;
  79. for(int i=1;i<16;i++){
  80. val[i]=val[i-1]*2;
  81. }
  82. int t;
  83. cin>>t;
  84. while(t--){
  85. int n;int q;
  86. cin>>n>>q;
  87. int x;
  88. Trie* myTrie = new Trie();
  89. for(int i=1;i<=n;i++){
  90. cin>>x;
  91. string temp;
  92. for(int j=0;j<16;j++){
  93. if(x&1){
  94. temp.push_back('1');
  95. }else{
  96. temp.push_back('0');
  97. }
  98. x/=2;
  99. }
  100. reverse(temp.begin(),temp.end());
  101. myTrie->TrieInsert(temp,i);
  102.  
  103. }
  104.  
  105.  
  106. while(q--){
  107. int a,l,r;
  108. cin>>a>>l>>r;string temp;
  109. for(int i=0;i<16;i++){
  110. if(a&1){
  111. temp.push_back('1');
  112. }else{
  113. temp.push_back('0');
  114. }
  115. a/=2;
  116. }
  117. reverse(temp.begin(),temp.end());
  118. cout<<myTrie->getMyMaxi(temp,l,r)<<"\n";
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. }
  129.  
  130.  
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement