大意: 给定n元素序列, m个询问$(l,r)$, 求$[l,r]$中选出任意数异或后的最大值
线性基沙茶题, 直接线段树暴力维护两个log还是能过的
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 5e5+10; int n, q, ql, qr;
int a[N]; struct _ {
int a[22];
void ins(int x) {
REP(i,1,*a) x=min(x,a[i]^x);
if (x) a[++*a]=x;
}
_ operator + (const _ &rhs) const {
_ r;
REP(i,0,*a) r.a[i]=a[i];
REP(i,1,rhs.a[0]) r.ins(rhs.a[i]);
return r;
}
} tr[N<<2]; void build(int o, int l, int r) {
if (l==r) return ({
int t;
scanf("%d", &t);
tr[o].ins(t);
});
build(ls),build(rs);
tr[o]=tr[lc]+tr[rc];
}
_ query(int o, int l, int r) {
if (ql<=l&&r<=qr) return tr[o];
if (mid>=qr) return query(ls);
if (mid<ql) return query(rs);
return query(ls)+query(rs);
} int main() {
scanf("%d", &n);
build(1,1,n);
scanf("%d", &q);
REP(i,1,q) {
scanf("%d%d",&ql,&qr);
auto t = query(1,1,n);
int ans = 0;
REP(i,1,t.a[0]) ans=max(ans,ans^t.a[i]);
printf("%d\n", ans);
}
}
考虑一下一个log的做法, 对于每个基维护一个最后出现的位置, 贪心尽量让高位的位置最大就好了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e5+10;
int n, q;
int a[N][21], p[N][21]; int main() {
scanf("%d", &n);
REP(i,1,n) {
int t;
scanf("%d", &t);
REP(j,0,20) a[i][j]=a[i-1][j],p[i][j]=p[i-1][j];
int pos = i;
PER(j,0,20) if (t>>j&1) {
if (p[i][j]<pos) swap(a[i][j],t),swap(p[i][j],pos);
if (!t) break;
t ^= a[i][j];
}
}
scanf("%d", &q);
REP(i,1,q) {
int l, r;
scanf("%d%d", &l, &r);
int ans = 0;
PER(j,0,20) if (p[r][j]>=l) ans=max(ans,a[r][j]^ans);
printf("%d\n", ans);
}
}