[BZOJ4184]shallot

题目

传送门

题解

似乎是一道线段树分治的板题,但是同时也使用了线性基。

考虑将时间建一棵线段树。

再者,一个区间里面,存在一个 \(x\) 和存在 \(n\)\(x\) 是没有区别的,所以我们只需要处理出一个 \(x\) 所存在的最大的一个区间,再在这个时间区间里插入 \(x\) 即可,最后用一个 \(dfs\) 按照左右顺序遍历线段树,在叶子节点输出询问即可。

#include<bits/stdc++.h>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T qread(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

struct liner_basic{
    #define MAXSIZE 31
    int f[MAXSIZE+5],siz,failed;
    liner_basic(){memset(f,0,sizeof f);siz=failed=0;}
    inline void Insert(int num){
        fep(i,MAXSIZE,0){
            if(!(num>>i))continue;
            if(!f[i]){f[i]=num;++siz;return;}
            num^=f[i];
        }
        ++failed;
    }
    inline int queryMax(int k){
        fep(i,MAXSIZE,0)if((k^f[i])>k)k^=f[i];
        return k;
    }
};

const int MAXN=500000;

map< int,pair<int,int> >MP;

int n;

vector<int>exist[MAXN<<2|2];

#define lc (i<<1)
#define rc (i<<1|1)
#define mid ((l+r)>>1)
#define _lq lc,l,mid
#define _rq rc,mid+1,r

inline void modify(const int L,const int R,const int x,const int i=1,const int l=1,const int r=n){
    if(L<=l && r<=R){
        exist[i].push_back(x);
        return;
    }
    if(L<=mid)modify(L,R,x,_lq);
    if(mid<R)modify(L,R,x,_rq);
}

void dfs(liner_basic G,const int i=1,const int l=1,const int r=n){
    if(!exist[i].empty())rep(j,0,exist[i].size()-1)G.Insert(exist[i][j]);
    if(l==r){
        printf("%d\n",G.queryMax(0));
        return;
    }
    dfs(G,_lq);
    dfs(G,_rq);
}

inline void Init(){
    cin>>n;int a;
    rep(i,1,n){
        cin>>a;
        if(a<0){a=-a;
            --MP[a].first;
            if(MP[a].first==0)modify(MP[a].second,i-1,a);
        }else{
            if(MP[a].first==0)MP[a].second=i;
            ++MP[a].first;
        }
    }
    for(auto it=MP.begin();it!=MP.end();++it)if((it->second).first>0)
        modify((it->second).second,n,it->first);
}

signed main(){
    Init();
    liner_basic G;
    dfs(G);
	return 0;
}
posted @ 2020-05-14 15:41  Arextre  阅读(200)  评论(0编辑  收藏  举报