Advertisement
Maruf_Hasan

Trie_4862ICPC

Jan 13th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1.  
  2. //In the Name of Allah Most Gracious, Most Merciful//
  3. /*If you want something you've never had, you have to do something you never did.*/
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8.  
  9. #define pb push_back
  10. #define ll long long
  11. #define pii pair<int,int>
  12. #define pll pair<ll,ll>
  13. #define M 100007
  14. #define INF 1e9
  15. #define INFL 1e18
  16. #define PI acos(-1)
  17. #define mp make_pair
  18.  
  19. //const int fx[]= {+1,-1,+0,+0};
  20. //const int fy[]= {+0,+0,+1,-1};
  21. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  22. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  23. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  24. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  25.  
  26.  
  27. struct node
  28. {
  29. int endmark;
  30. node* next[2];
  31. node()
  32. {
  33. endmark=0;
  34. next[0]=NULL;
  35. next[1]=NULL;
  36. }
  37. }*root;
  38.  
  39.  
  40.  
  41. void insert_node(int num)
  42. {
  43. node* curr=root;
  44. for(int i=31;i>=0;i--)
  45. {
  46. int rem=(num&(1<<i))!=0;
  47. if(curr->next[rem]==NULL)
  48. {
  49. curr->next[rem]=new node();
  50. }
  51. curr=curr->next[rem];
  52. curr->endmark++;
  53. }
  54. }
  55.  
  56.  
  57. ll query_node(int num)
  58. {
  59. ll sum=0;
  60. node* curr=root;
  61. for(int i=31;i>=0;i--)
  62. {
  63. int rem=!(((num)&(1<<i))!=0);
  64. // cout<<rem<<endl;
  65. if(curr->next[rem]!=NULL)
  66. {
  67. sum+=(1<<i);
  68. curr=curr->next[rem];
  69. }
  70. else
  71. {
  72. curr=curr->next[!rem];
  73. }
  74. }
  75. return sum;
  76. }
  77.  
  78.  
  79. void delete_node(node *curr)
  80. {
  81. for(int i=0;i<2;i++)
  82. {
  83. if(curr->next[i]!=NULL)
  84. {
  85. delete_node(curr->next[i]);
  86. }
  87. }
  88. delete (curr);
  89. }
  90. int main()
  91. {
  92.  
  93. // #ifndef ONLINE_JUDGE
  94. // freopen("input.txt","r",stdin);
  95. // freopen("output.txt","w",stdout);
  96. // #endif
  97. int t;
  98. cin>>t;
  99. while(t--)
  100. {
  101.  
  102. root=new node();
  103. int n;
  104. cin>>n;
  105. int arr[n+5];
  106. for(int i=0;i<n;i++)
  107. {
  108. cin>>arr[i];
  109. }
  110. int pre=0;
  111. ll ans=0;
  112. insert_node(0);
  113. for(int i=0;i<n;i++)
  114. {
  115. pre=pre^arr[i];
  116. insert_node(pre);
  117. // cout<<pre<<endl;
  118. // cout<<query_node(pre)<<endl;
  119. ans=max(ans,query_node(pre));
  120. // cout<<ans<<endl;
  121. }
  122. cout<<ans<<" ";
  123. delete_node(root);
  124.  
  125. }
  126. cout<<endl;
  127. ///Before submit=>
  128. /// *check for integer overflow,array bounds
  129. /// *check for n=1
  130. }
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement