欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-10 23:14:24
题意 "题目链接" Sol 记$s_i$表示前$i$个数的前缀异或和,我们每次相当于要找一个$j$满足$0 define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second // define int ......

题意

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;
}