Cod sursa(job #2886243)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 7 aprilie 2022 14:48:07
Problema Xor Max Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>
#define nmax 1000001
#define bitmax 22
using namespace std;

ifstream f("xormax.in");
ofstream g("xormax.out");

struct tr{

    int nr=-1;
    tr *next[2]={NULL,NULL};
    tr(){
        next[0]=NULL;
        next[1]=NULL;

    }
};
tr root;

void ins(const int x, const int poz,tr *nod, int l)
{
    if(l==-1) {nod->nr=poz;return;}
    bool dir=((x&(1<<l))>0);
    if(nod->next[dir]==NULL) nod->next[dir]=new tr();
    ins(x,poz,nod->next[dir],l-1);
}


int qer(const int x,tr *nod, int l)
{
    if(l==-1) {return nod->nr;}
    bool dir=((x&(1<<l))==0);
    //g<<dir;
    if(nod->next[dir]==NULL) return qer(x,nod->next[1-dir],l-1);
    return qer(x,nod->next[dir],l-1);
}

void debug(tr *nod, int l, int ans)
{
    if(l==-1)
    {
        g<<ans<<'\n';
    }
    if(nod->next[0]!=NULL) debug(nod->next[0],l-1,(ans<<1));
    if(nod->next[1]!=NULL) debug(nod->next[1],l-1,(ans<<1)+1);
}

int v[nmax];
int n;
int main()
{
    f>>n;
    ins(0,0,&root,bitmax);
    //debug(&root,bitmax,0);
    //g<<'\n';
    int ans=0,st=0,dr=0;
    for(int i=1;i<=n;i++)
    {
        int nr; f>>nr;
        v[i]=v[i-1]^nr;
        int opp=qer(v[i],&root,bitmax);
        //g<<i<<','<<opp<<'\n';
        if(ans<(v[i]^v[opp]))
        {
            ans=(v[i]^v[opp]);
            st=opp+1;
            dr=i;
        }
        ins(v[i],i,&root,bitmax);
    }
    g<<ans<<' '<<st<<' '<<dr;
    return 0;
}