Cod sursa(job #1291072)

Utilizator RaduVisanRadu Visan RaduVisan Data 12 decembrie 2014 10:30:32
Problema Xor Max Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int NMAX = 100010;

int N, V[NMAX], Max, Start, Stop, Now;

struct Trie
{
    Trie *Son[2];

    Trie()
    {
        Son[0] = Son[1] = 0;
    }
};

Trie *T = new Trie;

void Insert(Trie *Node, int Val, int Bit)
{
    if(Bit == 0) return ;
    int NowBit = (Val >> Bit) & 1;

    if(Node -> Son[NowBit] == 0) Node -> Son[NowBit] = new Trie;
    Insert(Node -> Son[NowBit], Val, Bit - 1);
}

void Query(Trie *Node, int Val, int Bit)
{
    if(Bit == 0) return ;
    int NowBit = (Val >> Bit) & 1;

    if(NowBit == 0)
    {
        if(Node -> Son[1] != 0)
            Now += (1 << Bit), Query(Node -> Son[1], Val, Bit - 1);
        else
            Query(Node -> Son[0], Val, Bit - 1);
    }else
    {
        if(Node -> Son[0] != 0)
            Now += (1 << Bit), Query(Node -> Son[0], Val, Bit - 1);
        else
            Query(Node -> Son[1], Val, Bit - 1);
    }
}

int main()
{
    freopen("xormax.in", "r", stdin);
    freopen("xormax.out", "w", stdout);

    scanf("%i", &N);
    int XorSum = 0;

    Insert(T, 0, 21);

    for(int i = 1; i <= N; ++ i)
    {
        scanf("%i", &V[i]);
        XorSum ^= V[i];
        Now = 0;
        Query(T, XorSum, 21);

        if(Now > Max) Max = Now, Stop = i;

        Insert(T, V[i], 21);
    }

    XorSum = 0;
    for(int i = Stop; i; -- i)
    {
        XorSum ^= V[i];
        if(XorSum == Max)
        {
            Start = i;
            break;
        }
    }

    printf("%i %i %i\n", Max, Start, Stop);
}