Cod sursa(job #3152845)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 26 septembrie 2023 21:32:15
Problema Xor Max Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.7 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;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

InParser 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);
    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";
    fout.close();
    return 0;
}