|
| 1 | +/* |
| 2 | + |
| 3 | + Name: Mehul Chaturvedi |
| 4 | + IIT-Guwahati |
| 5 | + |
| 6 | +*/ |
| 7 | + |
| 8 | +/* |
| 9 | + PROBLEM STATEMENT |
| 10 | +Given an array of n integers, find subarray whose xor is maximum. |
| 11 | +Input Format |
| 12 | +First line contains single integer n (1<=n<=1000000). |
| 13 | +Next line contains n space separated integers. |
| 14 | +Output Format |
| 15 | +Print xor of the subarray whose xor of all elements in subarray is maximum over all subarrays. |
| 16 | +Constraints |
| 17 | +1 <= n <= 1000000 |
| 18 | +1 <= A[i] <=100000 |
| 19 | +Sample Input |
| 20 | +4 |
| 21 | +1 2 3 4 |
| 22 | +Sample Output |
| 23 | +7 |
| 24 | +*/ |
| 25 | + |
| 26 | +#include <bits/stdc++.h> |
| 27 | +using namespace std; |
| 28 | + |
| 29 | +typedef long long ll; |
| 30 | +typedef unordered_map<int, int> umapii; |
| 31 | +typedef unordered_map<int, bool> umapib; |
| 32 | +typedef unordered_map<string, int> umapsi; |
| 33 | +typedef unordered_map<string, string> umapss; |
| 34 | +typedef map<string, int> mapsi; |
| 35 | +typedef map<pair<int, int>, int> mappiii; |
| 36 | +typedef map<int, int> mapii; |
| 37 | +typedef pair<int, int> pii; |
| 38 | +typedef pair<long long, long long> pll; |
| 39 | +typedef unordered_set<int> useti; |
| 40 | + |
| 41 | +#define uset unordered_set |
| 42 | +#define it iterator |
| 43 | +#define mp make_pair |
| 44 | +#define pb push_back |
| 45 | +#define all(x) (x).begin(), (x).end() |
| 46 | +#define f first |
| 47 | +#define s second |
| 48 | +#define MOD 1000000007 |
| 49 | + |
| 50 | +#include<bits/stdc++.h> |
| 51 | +using namespace std; |
| 52 | + |
| 53 | +// Assumed int size |
| 54 | +#define INT_SIZE 32 |
| 55 | + |
| 56 | +// A Trie Node |
| 57 | +struct TrieNode |
| 58 | +{ |
| 59 | + int value; // Only used in leaf nodes |
| 60 | + TrieNode *arr[2]; |
| 61 | +}; |
| 62 | + |
| 63 | +// Utility function tp create a Trie node |
| 64 | +TrieNode *newNode() |
| 65 | +{ |
| 66 | + TrieNode *temp = new TrieNode; |
| 67 | + temp->value = 0; |
| 68 | + temp->arr[0] = temp->arr[1] = NULL; |
| 69 | + return temp; |
| 70 | +} |
| 71 | + |
| 72 | +// Inserts pre_xor to trie with given root |
| 73 | +void insert(TrieNode *root, int pre_xor) |
| 74 | +{ |
| 75 | + TrieNode *temp = root; |
| 76 | + |
| 77 | + // Start from the msb, insert all bits of |
| 78 | + // pre_xor into Trie |
| 79 | + for (int i=INT_SIZE-1; i>=0; i--) |
| 80 | + { |
| 81 | + // Find current bit in given prefix |
| 82 | + bool val = pre_xor & (1<<i); |
| 83 | + |
| 84 | + // Create a new node if needed |
| 85 | + if (temp->arr[val] == NULL) |
| 86 | + temp->arr[val] = newNode(); |
| 87 | + |
| 88 | + temp = temp->arr[val]; |
| 89 | + } |
| 90 | + |
| 91 | + // Store value at leaf node |
| 92 | + temp->value = pre_xor; |
| 93 | +} |
| 94 | + |
| 95 | +// Finds the maximum XOR ending with last number in |
| 96 | +// prefix XOR 'pre_xor' and returns the XOR of this maximum |
| 97 | +// with pre_xor which is maximum XOR ending with last element |
| 98 | +// of pre_xor. |
| 99 | +int query(TrieNode *root, int pre_xor) |
| 100 | +{ |
| 101 | + TrieNode *temp = root; |
| 102 | + for (int i=INT_SIZE-1; i>=0; i--) |
| 103 | + { |
| 104 | + // Find current bit in given prefix |
| 105 | + bool val = pre_xor & (1<<i); |
| 106 | + |
| 107 | + // Traverse Trie, first look for a |
| 108 | + // prefix that has opposite bit |
| 109 | + if (temp->arr[1-val]!=NULL) |
| 110 | + temp = temp->arr[1-val]; |
| 111 | + |
| 112 | + // If there is no prefix with opposite |
| 113 | + // bit, then look for same bit. |
| 114 | + else if (temp->arr[val] != NULL) |
| 115 | + temp = temp->arr[val]; |
| 116 | + } |
| 117 | + return pre_xor^(temp->value); |
| 118 | +} |
| 119 | + |
| 120 | +// Returns maximum XOR value of a subarray in arr[0..n-1] |
| 121 | +int maxSubarrayXOR(int arr*, int n) |
| 122 | +{ |
| 123 | + // Create a Trie and insert 0 into it |
| 124 | + TrieNode *root = newNode(); |
| 125 | + insert(root, 0); |
| 126 | + |
| 127 | + // Initialize answer and xor of current prefix |
| 128 | + int result = INT_MIN, pre_xor =0; |
| 129 | + |
| 130 | + // Traverse all input array element |
| 131 | + for (int i=0; i<n; i++) |
| 132 | + { |
| 133 | + // update current prefix xor and insert it into Trie |
| 134 | + pre_xor = pre_xor^arr[i]; |
| 135 | + insert(root, pre_xor); |
| 136 | + |
| 137 | + // Query for current prefix xor in Trie and update |
| 138 | + // result if required |
| 139 | + result = max(result, query(root, pre_xor)); |
| 140 | + } |
| 141 | + return result; |
| 142 | +} |
| 143 | + |
| 144 | +int main( int argc , char ** argv ) |
| 145 | +{ |
| 146 | + ios_base::sync_with_stdio(false) ; |
| 147 | + cin.tie(NULL) ; |
| 148 | + |
| 149 | + int n; |
| 150 | + cin>>n; |
| 151 | + int* arr = new int[n]; |
| 152 | + for (int i = 0; i < n; ++i) |
| 153 | + { |
| 154 | + cin>>arr[i]; |
| 155 | + } |
| 156 | + |
| 157 | + cout << maxSubarrayXOR(arr, n) << '\n'; |
| 158 | + |
| 159 | + return 0 ; |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +} |
0 commit comments