Pagini recente » Cod sursa (job #1222797) | Cod sursa (job #1904760) | Cod sursa (job #1674382) | Cod sursa (job #1553915) | Cod sursa (job #1313627)
#include<fstream>
#include<vector>
#define SIZE 3
using namespace std;
ifstream fin("xormax.in");
ofstream fout("xormax.out");
struct Nod {
int val;
Nod *next[2];
Nod() {
val = 0;
next[0] = next[1] = NULL;
}
};
typedef Nod* pNod;
pNod root = new Nod();
vector<int> SUM;
int n, elem, xormax, b, e, maxi;
bool BITS[17];
vector<bool> XOR;
void getBits(int n) {
for(int i=SIZE; i; i--) {
BITS[i] = n%2;
n /= 2;
}
}
void addToTree(int i) {
pNod node = root;
for(int j=0; j<=SIZE; j++) {
bool bit = BITS[j];
pNod next = node -> next[bit];
if(next == NULL) {
next = new Nod();
node -> next[bit] = next;
}
node = node -> next[bit];
}
if(node -> val == 0) {
node -> val = i;
}
}
void findXor(pNod root1, pNod root2, int X1, int X2) {
if(root1 -> next[1] == NULL and root1 -> next[0] == NULL) {
if(xormax < (X1 xor X2)) {
xormax = (X1 xor X2);
b = root1 -> val;
e = root2 -> val;
if(b > e) swap(b, e);
b++;
}
return;
}
bool good = false;
if(root1 -> next[0] and root2 -> next[1]) {
findXor(root1 -> next[0], root2 -> next[1], 2*X1, 2*X2+1);
good = true;
}
if(root1 -> next[1] and root2 -> next[0]) {
findXor(root1 -> next[1], root2 -> next[0], 2*X1+1, 2*X2);
good = true;
}
if(!good) {
if(root1 -> next[0])
findXor(root1 -> next[0], root2 -> next[0], 2*X1, 2*X2);
else
findXor(root1 -> next[1], root2 -> next[1], 2*X1+1, 2*X2+1);
}
}
int main() {
fin>>n;
SUM.push_back(0);
for(int i=1; i<=n; i++) {
fin>>elem;
SUM.push_back(SUM[i-1] xor elem);
if(maxi < SUM[i]) {
maxi = SUM[i];
b = e = i;
}
}
xormax = maxi;
for(int i=1; i<=n; i++) {
getBits(SUM[i]);
addToTree(i);
}
findXor(root, root, 0, 0);
fout<<xormax<<" "<<b<<" "<<e;
return 0;
}