Cod sursa(job #2930192)

Utilizator YosifIosif Andrei Stefan Yosif Data 27 octombrie 2022 18:16:02
Problema Xor Max Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 6.7 kb
#include <bits/stdc++.h>
using namespace std;

class input {

private:

    FILE* fin;
    const int sz = 10000;
    char* buff, c;
    int p;

    char read()
    {
        if (p == sz)
        {
            fread(buff, 1, sz, fin);
            p = 0;
            return buff[p++];
        }
        else
            return buff[p++];
    }

public:
    
    input(const char* name)
    {
        c = '-';
        buff = new char[sz];
        p = sz;
        fin = fopen(name, "r");
    }

    void close()
    {
        fclose(fin);
    }

    void open(const char* name)
    {
        c = '-';
        p = sz;
        fin = fopen(name, "r");
    }

    input& operator >> (int& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (long long& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (unsigned long long& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (char& ch)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        ch = c;

        return *this;
    }

    input& operator >> (string& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = "";

        while (c != ' ' && c != '\n' && c != '\0')
            n += c, c = read();

        return *this;
    }

    input& operator >> (double& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        while (isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        if (c != '.')
            return *this;

        c = read();

        double p10 = 10;

        while (isdigit(c))
            n = n + double(c - '0') / p10, c = read(), p10 *= 10;

        return *this;
    }

    void getline(string& s)
    {
        s = "";

        c = read();

        while (c == ' ')
            c = read();

        while (c != '\n')
            s += c, c = read();
    }
};
class output {

private:
    FILE* fout;
    const int sz = 10000;
    char* buff;
    int p;

    void write(char ch)
    {
        if (p == sz)
        {
            fwrite(buff, 1, sz, fout);
            p = 0;
            buff[p++] = ch;
        }
        else
            buff[p++] = ch;
    }

public:

    output(const char* name)
    {
        buff = new char[sz];
        p = 0;
        fout = fopen(name, "w");
    }

    ~output()
    {
        fwrite(buff, 1, p, fout);
    }

    output& operator << (int n)
    {
        if (n < 0)
            write('-'), n *= -1;

        if (n == 0)
        {
            write(n + '0');
            return *this;
        }

        if (n / 10 != 0)
            *this << (n / 10);

        write(('0' + (n % 10)));

        return *this;
    }

    output& operator << (long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }

        if (n < 10)
        {
            write(n + '0');
            return *this;
        }

        *this << (n / 10);
        write((n % 10) + '0');

        return *this;
    }

    output& operator << (unsigned long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }

        if (n < 10)
        {
            write(n + '0');
            return *this;
        }

        *this << (n / 10);
        write((n % 10) + '0');

        return *this;
    }

    output& operator << (char c)
    {
        write(c);
        return *this;
    }

    void precision(double n, int x)
    {
        *this << int(n);

        if (!x)
            return;

        write('.');

        n -= int(n);
        n *= 10;

        for (int i = 1; i <= x; i++)
        {
            *this << int(n);
            n -= int(n);
            n *= 10;
        }
    }

    output& operator << (string n)
    {
        for (auto i : n)
            write(i);
        return *this;
    }
};

input fin("number.in");
output fout("number.out");

#define null 0

struct nod {
    int bit;

    nod* left, * right;
};

nod * root;
int n, r;
bitset <21> x;

void init(nod* p)
{
    if (p->bit == 0)
        return;

    p->left = new nod();
    p->left->bit = p->bit - 1;
    p->left->left = p->left->right = null;

    init(p->left);
}

void sub(nod* p)
{
    if (p->bit == 0)
        return;

    if (p->left != null && p->right != null)
    {
        r += (1 << (p->bit - 1));

        if (x[p->bit - 1])
            sub(p->left);
        else
            sub(p->right);

        return;
    }

    if (p->left != null)
    {
        if (x[p->bit - 1])
            r += (1 << (p->bit - 1));

        sub(p->left);
    }
    else
    {
        if (!x[p->bit - 1])
            r += (1 << (p->bit - 1));

        sub(p->right);
    }
}

void add(nod* p)
{
    if (p->bit == 0)
        return;

    if (x[p->bit - 1])
    {
        if (p->right == null)
        {
            p->right = new nod();
            p->right->bit = p->bit - 1;
            p->right->left = p->right->right = null;
        }
        
        add(p->right);
    }
    else
    {
        if (p->left == null)
        {
            p->left = new nod();
            p->left->bit = p->bit - 1;
            p->left->right = p->left->left = null;
        }

        add(p->left);
    }
}

int main() {

    root = new nod();
    root->bit = 21;
    root->left = root->right = null;
    init(root);
    
    fin >> n;
    int sum = 0, maxi = 0;

    for (int i = 1; i <= n; i++)
    {
        int val;
        fin >> val;
        sum ^= val;

        r = 0;
        x = sum;
        sub(root);
        maxi = max(maxi, r);
        add(root);
    }

    fout << maxi;
    
    return 0;
}