noi.ac#309 Mas的童年(子集乱搞)

题意

题目链接

Sol

\(s_i\)表示前\(i\)个数的前缀异或和,我们每次相当于要找一个\(j\)满足\(0 < j < i\)\((s_i \oplus s_j) + s_j\)最大

然后下面的就和标算相差十万八千里了。

\[\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} \]

也就是对于每个\(i\),我们要在前面找一个\(j\)使得\(\text{~}s[i] \& s[j]\)最大

然后这里暴力处理子集就行了(一开始还想了半天trie树)。

加一个记忆化可以保证复杂度

最后复杂度为\(O(2^{20} + n \log{a_i})\)

#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;
}
posted @ 2019-03-30 15:07  自为风月马前卒  阅读(554)  评论(0编辑  收藏  举报

Contact with me