E. Sausage Maximization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages!
In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (the xor operation) of all integers in that sausage.
One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage.
But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces).
The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero.
Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens.
Input
The first line contains an integer n (1 ≤ n ≤ 105).
The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 1012) — Mr. Bitkoch's sausage.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Output
Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.
Examples
input
Copy
2 1 2output
Copy
3input
Copy
3 1 2 3output
Copy
3input
Copy
2 1000 1000output
Copy
1000
題目大意:給你一列數字,要你求出這列數字的前綴異或值與後綴異或值 異或的最大值,要求前後綴不能相交,前後綴可以爲空,即爲0。前綴異或值:指前i(0<=i<n)項的所有數異或後的值,後綴異或值同理。
解題思路:預處理出前綴異或,後綴異或。每一次把前綴異或插入到01字典樹當中,長度加1的後綴異或在樹上找異或後值最大的前綴異或即可。
/* @Author: Top_Spirit @Language: C++ */ #include <bits/stdc++.h> using namespace std ; typedef unsigned long long ull ; typedef long long ll ; const int Maxn = 1e5 + 10 ; const int INF = 0x3f3f3f3f ; const int base = 64 ; const ull seed = 133 ; const double PI = acos(-1.0) ; ll n ; ll Next[Maxn * 50][2], cnt ; ll pre[Maxn], suf[Maxn] ; ll a[Maxn] ; void init(){ cnt = 0 ; memset(Next, 0, sizeof(Next)) ; } void Insert(ll k){ bitset < base > bit = k ; int p = 0 ; for (int i = 63; i >= 0; i--){ int id = bit[i] ; // int id = (k & (1ll << i)) > 0 ; if (!Next[p][id]) Next[p][id] = ++cnt ; p = Next[p][id] ; } } ll Find(ll k) { bitset < base > bit = k ; int p = 0 ; ll ans = 0 ; for (int i = 63; i >= 0; i--){ int id = bit[i] ; // int id = (k & (1ll << i)) > 0 ; if (Next[p][id ^ 1]){ p = Next[p][id ^ 1] ; id = id ^ 1 ; } else p = Next[p][id] ; ans += (1ll * (id)) << i ; } return ans ; } int main (){ cin >> n ; init() ; pre[0] = 0; suf[n + 1] = 0 ; for (int i = 1; i <= n; i++){ cin >> a[i] ; pre[i] = pre[i- 1] ^ a[i] ; } for (int i = n; i >= 1; i--) suf[i] = suf[i + 1] ^ a[i] ; ll ans= 0, tmp = 0 ; for (int i = 0; i <= n + 1; i++){ Insert(pre[i]) ; tmp = Find(suf[i + 1]) ; // cout << pre[i] << " " << tmp << endl ; ans = max(ans, tmp ^ suf[i + 1]) ; } cout << ans << endl ; return 0; }