《洛谷CF1100F Ivan and Burgers》

显然第一眼用线段树合并线性基即可。

对于线段树复杂度nlogn。合并logn,插入logn.。

总复杂度在nlogn^3。对于这里的5e5,依旧会超时。

考虑其他的思路:这里用了和某道树上查询的紫题差不多的思路。

我们让线性基尽可能靠右,然后只需要判断是否在区间内即可。

因为预处理出了所有点,那么查询只需要logn查询即可。

然后更新也只需要在前点基础上更新,那么更新也在logn复杂度。

那么总复杂nlogn即可通过本题。

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 5e5+5;
const int M = 1e4;
const LL Mod = 2008;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int n,a[N],p[N][21],pos[N][21];
void insert(int u,int *p,int *pos)
{
    int x = a[u];
    for(rg int i = 20;i >= 0;--i)
    {
        if(((x>>i)&1) == 0) continue;
        if(p[i] == 0)
        {
            p[i] = x;
            pos[i] = u;
            return ;
        }
        if(u > pos[i])
        {
            swap(pos[i],u);
            swap(p[i],x);
        }
        x ^= p[i];
    }
}
int main()
{
    n = read();
    for(rg int i = 1;i <= n;++i) a[i] = read();
    for(rg int i = 1;i <= n;++i)
    {
        for(rg int j = 20;j >= 0;--j) p[i][j] = p[i-1][j],pos[i][j] = pos[i-1][j];
        insert(i,p[i],pos[i]);
    }
    int q;q = read();
    while(q--)
    {
        int L,r;L = read(),r = read();
        int base[21];
        for(rg int i = 20;i >= 0;--i)
        {
            if(pos[r][i] >= L) base[i] = p[r][i];
            else base[i] = 0;
        }
        int ans = 0;
        for(rg int i = 20;i >= 0;--i) ans = max(ans,ans^base[i]);
        printf("%d
",ans);
    }
    system("pause");
}
View Code

现在回过头来看,对于线段树的做法,是否可能压到nlogn^2呢。

我们可以将序列看成树链,那么就可以点分治来压到nlogn^2.

代码待补

原文地址:https://www.cnblogs.com/zwjzwj/p/13569544.html