1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <bits/stdc++.h> #define fir first #define sec second #define MAX_N 500000 #define mid ((s+t)>>1) using namespace std; typedef map<int,int>::iterator IT; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n; map <int, int> mrk, lst; vector <int> tr[MAX_N<<2]; struct Basis {int b[31];} init; void insert(Basis &B, int x) { for (int i = 30; ~i; i--) if (x>>i&1) { if (B.b[i]) x ^= B.b[i]; else {B.b[i] = x; break;} } } int query(Basis B) { int ret = 0; for (int i = 30; ~i; i--) ret = max(ret, ret^B.b[i]); return ret; } void insert(int v, int s, int t, int l, int r, int x) { if (s >= l && t <= r) {tr[v].push_back(x); return;} if (l <= mid) insert(v<<1, s, mid, l, r, x); if (r >= mid+1) insert(v<<1|1, mid+1, t, l, r, x); } void bi_solve(int v, int s, int t, Basis B) { for (int i = 0; i < (int)tr[v].size(); i++) insert(B, tr[v][i]); if (s == t) {printf("%d\n", query(B)); return;} bi_solve(v<<1, s, mid, B), bi_solve(v<<1|1, mid+1, t, B); } int main() { read(n); for (int i = 1; i <= n; i++) { int x; read(x); if (x > 0) {if (!mrk[x]++) lst[x] = i;} else {if (!--mrk[-x]) insert(1, 1, n, lst[-x], i-1, -x);} } for (IT it = mrk.begin(); it != mrk.end(); it++) if (it->sec) insert(1, 1, n, lst[it->fir], n, it->fir); return bi_solve(1, 1, n, init), 0; }
|