Cod sursa(job #1873654)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 9 februarie 2017 12:15:05
Problema Xor Max Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>

using namespace std;
int n,i,step,x,st,dr,sol=-1,k,v[1<<17];
struct trie
{
    int ind,a[2];
}w[1<<21];
int main()
{
    ifstream f("xormax.in");
    ofstream g("xormax.out");
    f>>n;
    for(i=0; i<=21; i++)
    {
        k++;
        w[i].a[0]=k;
    }
    for(i=1; i<=n; i++)
    {
        f>>v[i];
        v[i]^=v[i-1];
        x=0;
        for(step=1<<20; step; step>>=1)
        {
            int bit=((v[i]&step)==0);
            if(!w[x].a[bit]) x=w[x].a[bit^1];
            else x=w[x].a[bit];
        }
        if(sol<(v[i]^v[w[x].ind]))
        {
            sol=v[i]^v[w[x].ind];
            st=w[x].ind;
            dr=i;
        }
        x=0;
        for(step=1<<20; step; step>>=1)
        {
            int bit=((v[i]&step)>0);
            if(!w[x].a[bit]) w[x].a[bit]=++k;
            x=w[x].a[bit];
        }
        w[x].ind=i;
    }
    g<<sol<<" "<<st+1<<" "<<dr<<'\n';
    f.close(); g.close();
    return 0;
}