Cod sursa(job #3152844)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 26 septembrie 2023 21:31:01
Problema Xor Max Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <bits/stdc++.h>

#pragma optimize GCC ("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

///#include <tryhardmode>
///#include <GODMODE::ON>

using namespace std;

ifstream fin ("xormax.in");
ofstream fout ("xormax.out");

const int NMAX=1e5+5;
int sp[NMAX];
int v[NMAX];

class Trie{
    public:
    Trie* sons[2];
    int ind;

    Trie()
    {
        for(int i=0;i<2;i++)
            sons[i]=NULL;
        ind=0;
    }
};

Trie* node;

void insert_value(Trie* &node,int x,int poz,int ind)
{
    int aux;
    if(x & (1<<poz))
        aux=1;
    else
        aux=0;
    if(node->sons[aux]==NULL)
        node->sons[aux]=new Trie();
    if(poz==0)
    {
        node->sons[aux]->ind=ind;
        return ;
    }
    else
        insert_value(node->sons[aux],x,poz-1,ind);
}

pair<int,int>get_value(Trie* node,int x,int poz,int mask)
{
    bool aux;
    if(x & (1<<poz))
        aux=1;
    else
        aux=0;
    if(poz==-1)
        return make_pair(mask,node->ind);
    else if(node->sons[!aux]!=NULL)
        return get_value(node->sons[!aux],x,poz-1,(mask | (1<<poz)));
    else
        return get_value(node->sons[aux],x,poz-1,(mask | (1<<poz))^(1<<poz));
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    int n,i,j;
    fin>>n;
    Trie* node=new Trie();

    insert_value(node,0,20,0);
    int best=-1,st=-1,dr=-1;
    for(i=1;i<=n;i++)
    {
        fin>>v[i];
        sp[i]=sp[i-1]^v[i];
        pair<int,int>memo=get_value(node,sp[i],20,0);
        if(best<memo.first)
        {
            best=memo.first;
            st=memo.second+1;
            dr=i;
        }
        insert_value(node,sp[i],20,i);
    }
    fout<<best<<" "<<st<<" "<<dr<<"\n";
    fin.close();
    fout.close();
    return 0;
}