当前位置: 首页 > 工具软件 > Sausage > 使用案例 >

Codeforces E. Sausage Maximization (01字典树)

高海阳
2023-12-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;
}

 

 类似资料: