Cod sursa(job #238305)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 1 ianuarie 2009 20:16:08
Problema Xor Max Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
using namespace std;

#include <bitset>
#include <vector>

#define pb push_back
#define f first
#define s second
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define mp make_pair

#define IN  "xormax.in"
#define OUT "xormax.out"
#define N_MAX 1<<18
#define B_MAX 20
#define bit(x) (1<<(B_MAX+1))-1

typedef pair<int,int> pi;
typedef vector< vector<pi> > VM;

int poz,next,N;
int X[N_MAX],V[1<<20],P[1<<20];
char L[1<<20];
string word;
VM A(1<<20);

void scan()
{
	freopen(IN,"r",stdin);
	freopen(OUT,"w",stdout);
	scanf("%d",&N);
	
	FOR(i,1,N)
		scanf("%d",&V[i]);
}		

void convert(int x)
{
	int size = B_MAX+1;
	FOR(i,0,B_MAX) word[i] = 0;
	for(;x;word[--size] = x&1,x >>= 1);
}

void add(int nod,int x)
{
	if(x==B_MAX+1)
	{
		P[nod] = poz;
		return;
	}	
	FOR(i,0,L[nod]-1)
		if(A[nod][i].s == word[x])
		{
			add(A[nod][i].f,x+1);
			return;
		}	
	A[nod].pb( mp(++next,word[x]) );
	add(next,x+1);
	++L[nod];
}

int querry(int nod,int x)
{
	if(x==B_MAX+1)
		return nod;
	if(L[nod]-1 && A[nod][1].s == word[x])
		return querry(A[nod][1].f,x+1);
	return querry(A[nod][0].f,x+1);	
}

void solve()
{
	word.resize(B_MAX+1);
	FOR(i,1,N)
		X[i] = X[i-1] ^ V[i];
	
	pair<int, pi> rez;
	rez.f = -1;	
	
	for(int i=N-1;i>=0;--i)
	{
		convert(X[i+1]);
		poz = i+1;
		add(0,0);
		
		convert(X[i] ^ bit(X[i]));
		int nod = querry(0,0);
		if( (X[ P[nod] ] ^ X[i]) > rez.f || ((X[ P[nod] ] ^ X[i]) == rez.f && rez.s.s > P[nod]))
		{
			rez.f = X[ P[nod] ] ^ X[i];
			rez.s.f = i+1;
			rez.s.s = P[nod];
		}	
	}	
	printf("%d %d %d\n",rez.f,rez.s.f,rez.s.s);
}

int main()
{
	scan();
	solve();
	return 0;
}