[CF587E]Duff as a Queen

题目

传送门

题解

首先考虑,如果我们没有这个区间修改怎么做?

只需要用线段树的思想,每个区间维护一个线性基,询问的时候合并线性基即可。

至于如何合并,其实暴力就可以了,具体实现如同:

friend liner_basic operator + (liner_basic lhs,const liner_basic rhs){
    rep(i,0,MAXSIZE)lhs.Insert(rhs.f[i]);
    return lhs;
}

似乎是十分简单的。

然而,这道题偏偏要给你这个修改。

那么我们考虑如何让这个修改影响尽量方便处理,这个时候我们想到异或的特性——一个数同时异或俩相同的数还是它自己。

定义 \(b[i]=a[i-1]\oplus a[i]\),那么对于一个区间 \([l,r]\) 异或上 \(x\),其实就只有 \(b[l]\)\(b[r+1]\) 受到了影响,我们就把他转换成单点异或一个值

那么,这个时候的 \(a[r-1]=a[r]\oplus b[r]\)\(a[r-2]=a[r-1]\oplus b[r-1]=a[r]\oplus b[r]\oplus b[r-1]\) ,以此类推,我们发现,设集合 \(A=\{b[l+1],b[l+2]\cdots b[r],a[r]\}\),集合 \(B=\{a[l],a[l+1]\cdots a[r]\}\),那么 \(B\) 可以由 \(A\) 中的某些数异或得到

由这个我们可以得到什么?\(B\) 的线性基和 \(A\) 等价。

那么,我们只需要知道 \(b[l+1],b[l+2]\cdots b[r]\) 的线性基,再插入一个 \(a[r]\),就可以得到 \(B\) 的线性基,也就是我们所想要求的线性基。

现在再来理一下我们想要干什么:

  • 对于 \(b[]\) 的维护,用线段树维护单点修改,区间查询
  • 对于 \(a[]\) 的维护,用线段树维护区间修改,单点查询

然后代码自然而然就可以完成了。

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

const int MAXN=2e5;

struct liner_basic{
    #define MAXSIZE 31
    int f[MAXSIZE+5],siz,failed;
    liner_basic(){memset(f,siz=failed=0,sizeof f);}
    inline void clear(){memset(f,siz=failed=0,sizeof f);}
    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;
    }
    friend liner_basic operator + (liner_basic lhs,const liner_basic rhs){
        rep(i,0,MAXSIZE)lhs.Insert(rhs.f[i]);
        return lhs;
    }
};

int n,q;

int a[MAXN<<2|2],b[MAXN+5],tag[MAXN<<2|2];
liner_basic B[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

void Bmodify(const int p,const int x,const int i=1,const int l=1,const int r=n+1){
    //对于 B 的修改——单点修改, 区间查询
    if(l==r){
        B[i].clear();
        B[i].Insert(x);
        return;
    }
    if(p<=mid)Bmodify(p,x,_lq);
    else Bmodify(p,x,_rq);
    B[i]=B[lc]+B[rc];
}

void pushdown(const int i){
    a[lc]^=tag[i],a[rc]^=tag[i];
    tag[lc]^=tag[i],tag[rc]^=tag[i];
    tag[i]=0;
}

void Amodify(const int L,const int R,const int x,const int i=1,const int l=1,const int r=n){
    //对于 a 的修改——区间修改, 单点查询
    if(L<=l && r<=R){
        a[i]^=x,tag[i]^=x;
        return;
    }
    if(tag[i])pushdown(i);
    if(L<=mid)Amodify(L,R,x,_lq);
    if(mid<R)Amodify(L,R,x,_rq);
}

liner_basic Bquery(const int L,const int R,const int i=1,const int l=1,const int r=n+1){
    liner_basic ret;
    if(L>R)return ret;
    if(L<=l && r<=R)return B[i];
    if(L<=mid)ret=ret+Bquery(L,R,_lq);
    if(mid<R)ret=ret+Bquery(L,R,_rq);
    return ret;
}

int Aquery(const int p,const int i=1,const int l=1,const int r=n){
    if(p<1 || p>n)return 0;
    if(l==r)return a[i];
    if(tag[i])pushdown(i);
    if(p<=mid)return Aquery(p,_lq);
    return Aquery(p,_rq);
}

void buildtre(const int i=1,const int l=1,const int r=n){
    if(l==r){
        cin>>a[i];
        return;
    }
    buildtre(_lq);
    buildtre(_rq);
}

inline void Init(){
    cin>>n>>q;
    buildtre();
    rep(i,1,n+1)b[i]=Aquery(i-1)^Aquery(i);
    rep(i,1,n+1)Bmodify(i,b[i]);//其实不需要管 n+1, 因为我们的询问不涉及 b[n+1], 但是此处为了方便复习, 还是打上
}

inline void Getquery(){
    int opt,l,r,t;
    while(q--){
        cin>>opt>>l>>r;
        if(opt==1){cin>>t;
            Amodify(l,r,t);
            b[l]^=t,b[r+1]^=t;
            Bmodify(l,b[l]);
            Bmodify(r+1,b[r+1]);
        }else{
            liner_basic G=Bquery(l+1,r);
            G.Insert(Aquery(r));
            cout<<(1<<G.siz)<<endl;
        }
    }
}

signed main(){
    Init();
    Getquery();
	return 0;
}
posted @ 2020-05-15 11:40  Arextre  阅读(197)  评论(0编辑  收藏  举报