題目連接c++
記\(s_i\)表示前\(i\)個數的前綴異或和,咱們每次至關於要找一個\(j\)知足\(0 < j < i\)且\((s_i \oplus s_j) + s_j\)最大spa
而後下面的就和標算相差十萬八千里了。debug
\[ \begin{aligned} &(s_i \oplus s_j) + s_j\\ =&(s_i \oplus s_j \oplus s_j) + ((s_i \oplus s_j) \& s_j )\\ =&(s_i + (\text{~}s_i \& s_j)) \end{aligned} \]code
也就是對於每一個\(i\),咱們要在前面找一個\(j\)使得\(\text{~}s[i] \& s[j]\)最大get
而後這裏暴力處理子集就好了(一開始還想了半天trie樹)。it
加一個記憶化能夠保證複雜度class
最後複雜度爲\(O(2^{20} + n \log{a_i})\)bug
#include<bits/stdc++.h> #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define fi first #define se second //#define int long long #define LL long long #define ull unsigned long long #define Fin(x) {freopen(#x".in","r",stdin);} #define Fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int MAXN = 3e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10; const double eps = 1e-9; template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;} template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;} template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;} template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} template <typename A> inline void debug(A a){cout << a << '\n';} template <typename A> inline LL sqr(A x){return 1ll * x * x;} template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;} template <typename A> A inv(A x) {return fp(x, mod - 2);} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N, a[MAXN], s[MAXN]; bool mark[MAXN]; void insert(int x) { //if(mark[x]) return ; mark[x] = 1; for(int i = 0; i < 20; i++) if((x >> i & 1) && (!mark[x ^ (1 << i)])) insert(x ^ (1 << i)); } int Query(int x) { int ans = 0; for(int i = 19; ~i; i--) if((x >> i & 1) && mark[ans | 1 << i]) ans |= 1 << i; return ans; } signed main() { //freopen("ex_childhood2.in", "r", stdin); N = read(); for(int i = 1; i <= N; i++) a[i] = read(), s[i] = s[i - 1] ^ a[i]; for(int i = 1; i <= N; i++) { // for(int j = i - 1; j >= 0; j--) chmax(ans, (s[i] ^ s[j]) + s[j]); //for(int j = i - 1; j >= 0; j--) chmax(ans, (~s[i]) & s[j]); int ans = Query(~s[i]); cout << s[i] + ans * 2 << ' '; insert(s[i]); } puts(""); return 0; }