Cod sursa(job #3340420)

Utilizator Alex_at_gameIustin-Alexandru Frateanu Alex_at_game Data 14 februarie 2026 11:14:21
Problema Xor Max Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.19 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
#define all(v) begin(v), end(v)
#define al(v, l, r) begin(v) + l, begin(v) + r + 1
#define pb push_back
#define pob pop_back
#define sz(v) (int)v.size()
#define fs first
#define sd second

constexpr int inf = 2e9;
constexpr ll infll = 4e18;
constexpr int N = 1e5 + 5;

int n, a[N], p[N];

struct trie_nod {
    vector<trie_nod*> v;
    vector<int> idxs;

    trie_nod(): v(2, nullptr) {}
};

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    freopen("xormax.in", "r", stdin);
    freopen("xormax.out", "w", stdout);

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        p[i] = p[i - 1] ^ a[i];
    }

    trie_nod *rad = new trie_nod();
    int maxa = 0;
    pair<int, int> pmax = {-1, -1};

    for (int i = 1; i <= n; ++i) {
        string s = bitset<22>(p[i]).to_string();
        trie_nod *nod = rad;

        for (char c : s) {
            if (nod->v[c - '0'] == nullptr) {
                nod->v[c - '0'] = new trie_nod();
            }

            nod = nod->v[c - '0'];
            nod->idxs.pb(i);
        }
    }

    for (int i = 1; i <= n; ++i) {
        string s = bitset<22>(p[i]).to_string();
        trie_nod *nod = rad;
        string tmp = "";

        for (char c : s) {
            if (nod->v[(c - '0') ^ 1] != nullptr) {
                nod = nod->v[(c - '0') ^ 1];
                tmp += to_string((c - '0') ^ 1);
            } else {
                nod = nod->v[c - '0'];
                tmp += c;
            }
        }

        int cbst = p[i] ^ stoi(tmp, 0, 2); ///stoi(string, pozitie_inceput, baza_de_numeratie)

        if (maxa < cbst) {
            maxa = cbst;
            pmax.fs = i;

            for (int x : nod->idxs) {
                if (x != i) {
                    pmax.sd = x;
                    break;
                }
            }

            if (pmax.fs > pmax.sd) {
                swap(pmax.fs, pmax.sd);
            }

            pmax.fs++;
        }
    }

    cout << maxa << " " << pmax.fs << " " << pmax.sd;
}