Cod sursa(job #2846853)

Utilizator NeganAlex Mihalcea Negan Data 9 februarie 2022 19:02:06
Problema Xor Max Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, a[100005];

struct Trie
{
    int poz;
    Trie *fiu[2];
    Trie()
    {
        poz = 0;
        fiu[1] = fiu[0] = 0;
    }
};

Trie *t = new Trie;
inline void Add(Trie *nod, int poz, int x, int cnt)
{
    if(cnt < 0)
    {
        nod -> poz = poz;
        return;
    }
    bool bit;
    bit = (x & (1 << cnt));
    if(nod -> fiu[bit] == 0)
        nod -> fiu[bit] = new Trie;
    Add(nod -> fiu[bit], poz, x, cnt - 1);
}

inline int Search(Trie *nod, int x, int cnt)
{
    if(cnt < 0)
        return nod -> poz;
    bool bit;
    bit = (x & (1 << cnt));
    if(nod -> fiu[1 - bit] != 0)
        return Search(nod -> fiu[1 - bit], x, cnt - 1);
    return Search(nod -> fiu[bit], x, cnt - 1);
}
int main()
{
    int i;
    fin >> n;
    for(i = 1;i <= n;i++)
        fin >> a[i];
    for(i = 2;i <= n;i++)
        a[i] = (a[i] ^ a[i - 1]);
    int ans = -1, p, soli, solj;
    Add(t, 0, 0, 22);
    for(i = 1;i <= n;i++)
    {
        p = Search(t, a[i], 22);
        if(ans < (a[i] ^ a[p]))
        {
            ans = (a[i] ^ a[p]);
            soli = p + 1;
            solj = i;
        }
        Add(t, i, a[i], 22);
    }
    fout << ans << " " << soli << " " << solj;
    return 0;
}