Cod sursa(job #2026332)

Utilizator toadehuPuscasu Razvan Stefan toadehu Data 24 septembrie 2017 12:28:54
Problema Xor Max Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <bits/stdc++.h>

using namespace std;

int n,st,dr,rasp,sp;

struct trie
{
    trie *z,*u;
    int who;
    trie ()
    {
        z=NULL;
        u=NULL;
    }
}*nod;
trie*insert (int x, int bit ,trie*nod)
{
    if (nod==NULL)
    {
        nod = new trie;
    }
    if (bit==-1)// am terminat toti biti
    {
        return nod;
    }
    int fin=x&(1<<bit);
    if (fin==0)
    {
        nod->z=insert(x,bit-1,nod->z);
    }
    else
    {
        nod->u=insert(x,bit-1,nod->u);
    }
}

int find (int val , int bit , trie *nod)
{
    if (bit==-1)
    {
        return nod->who;
    }
    int fin=val&(1<<bit);//fin==0 sau 2
    if (fin==0)
    {
        if (nod->u!=NULL)
        {
            return find(val,bit-1,nod->u);
        }
        else
        {
            return find(val,bit-1,nod->z);
        }
    }
    else
    {
        if (nod->z!=NULL)
        {
            return find (val,bit-1,nod->z);
        }
        else
        {
            return find (val,bit-1,nod->u);
        }
    }
}

int main()
{
    ifstream fin ("xormax.in");
    ofstream fout ("xormax.out");
    fin>>n;
    for(int i = 1; i <= n; ++ i)
    {
        int x, pos = 0;
        fin>>x;
        if(i > 1)
        {
            sp = sp ^ x;
            int posibil = find(pos, sp, nod);
            if(posibil > rasp)
            {
                rasp = posibil;
                st = pos;
                dr = i;
            }
        }
        else
        {
            rasp = x;
            sp = x;
            st = 0;
            dr = 1;
        }
        insert(sp, i, nod);
    }
    fout<<rasp<<" "<<st+1<<" "<dr;
}