Codeforces E. Sausage Maximization (01字典树)

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 2

output

Copy

3

input

Copy

3
1 2 3

output

Copy

3

input

Copy

2
1000 1000

output

Copy

1000

题目大意:给你一列数字,要你求出这列数字的前缀异或值与后缀异或值  异或的最大值,要求前后缀不能相交,前后缀可以为空,即为0。前缀异或值:指前i(0<=i<n)项的所有数异或后的值,后缀异或值同理。

解题思路:预处理出前缀异或,后缀异或。每一次把前缀异或插入到01字典树当中,长度加1的后缀异或在树上找异或后值最大的前缀异或即可。

题目链接:https://codeforces.com/problemset/problem/282/E

/*
@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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值