Codechef REBXOR HYSBZ - 4260 (01 dictionary tree + interval exclusive or maximum)

Keywords: Programming


Input
The first line of input data contains an integer N, representing the number of elements in the array.
The second line contains N integers A1,A2,... AN.
Output
The output line contains the maximum possible value of a given expression.
Sample Input
5
1 2 3 1 2
Sample Output
6
Hint
The conditions satisfying (l1,r1,l2,r2) are: (1, 2, 3, 3), (1, 2, 4, 5), (3, 3, 4, 5).

For 100% data, 2 < N < 4 * 105, 0 < Ai < 109.
Several questions for finding interval difference or maximum are given. To find the interval exclusive or maximum is to find a prefix, and then the common 01 dictionary tree.
Like this topic, find two disjoint intervals, and then the exclusive or maximum of the two intervals.
We want to find the exclusive or maximum value of a prefix interval and record it in an array, then enumerate the interval of the suffix, and then update the maximum value.
The code is as follows:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#define ll long long
using namespace std;

const int maxx=4e5+100;
struct node{
	int trie[2];
	int num;
}p[maxx*33];
int sum[maxx],pre[maxx];
int a[maxx],dp[maxx];
int n,tot;

inline void Insert(int x)
{
	int root=0;
	int id;
	for(int i=32;i>=0;i--)
	{
		id=(x>>i)&1;
		if(!p[root].trie[id]) p[root].trie[id]=++tot;
		root=p[root].trie[id];
	}
	p[root].num=x;
}
inline int Find(int x)
{
	int root=0;
	int id;
	for(int i=32;i>=0;i--)
	{
		id=(x>>i)&1;
		if(p[root].trie[id^1]) root=p[root].trie[id^1];
		else root=p[root].trie[id];
	}
	return p[root].num^x;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		tot=0;
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++) sum[i]=sum[i-1]^a[i];//prefix
		for(int i=n;i>=1;i--) pre[i]=pre[i+1]^a[i];//Suffix
		Insert(0);
		for(int i=1;i<=n;i++)
		{
			dp[i]=max(dp[i-1],Find(sum[i]));
			Insert(sum[i]);
		}
		int _max=0;
		for(int i=n;i>=1;i--)
		{
			_max=max(_max,Find(pre[i])+dp[i-1]);//Enumeration of suffix interval exclusive or maximum plus prefixes previously calculated, their and de-update maximum
			Insert(pre[i]);
		}
		printf("%d\n",_max);
	}
	return 0;
}

Try to refuel a, (o)/~

Posted by sethupathy on Sun, 06 Oct 2019 11:27:47 -0700