bzoj 2741: 【FOTILE模拟赛】L

题意:在线询问区级最大异或和。

题解:

分块+可持久化trie。
第一次写可持久化trie,原来还有这种操作。
先求出前缀的异或和。那么原题就变成了在一段区间选两个数,异或值最大。
先考虑确定一个值的情况。
那另一个值就是在这段区间的数的二进制下的trie上尽量走相反的路线。
用可持久化trie就好,跟主席树差不多,自己YY的模板,贼慢。
然后分块求答案。f[i][j] 表示第i块的第一个到j的答案。
转移就用a[j]的答案与f[i][j1]取max就好了。
询问的时候找到最近的一块,其他暴力求n
细节较多要注意。
code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#define LL long long
using namespace std;
struct trnode{
    int a[3],c;
    bool tail;
    trnode(){tail=false;}
}tr[600000];int tot=0,root[12010];
int n,m,belong[12010];
LL a[12010];
LL f[120][12010];
LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void add(int &x,int froot,LL k,int num)
{
    x=++tot;
    tr[x]=tr[froot];tr[x].c++;
    if(num==-1) return;
    LL c=(k&(1LL<<num));
    c=(c!=0);
    add(tr[x].a[c],tr[froot].a[c],k,num-1);
}
LL solve(int lroot,int rroot,LL k,int num)
{
    if(num==-1) return 0;
    LL c=(k&(1LL<<num));
    c=(c!=0);c^=1;
    int lrc=tr[tr[lroot].a[c]].c,rrc=tr[tr[rroot].a[c]].c;
    if(lrc==rrc) c^=1;
    return (solve(tr[lroot].a[c],tr[rroot].a[c],k,num-1))+(c<<num);
}
int main()
{
    scanf("%d %d",&n,&m);
    a[0]=a[1]=0;
    add(root[1],root[0],(LL)0,33);
    for(int i=1;i<=n;i++)
    {
        a[i+1]=read();
        a[i+1]^=a[i];
        add(root[i+1],root[i],(LL)a[i+1],33);
    }
    int len=sqrt(n+1);
    memset(f,-1,sizeof(f));
    for(int i=2;i<=n+1;i++)
    {
        belong[i]=(i/len)+1;
        if(i==2||belong[i]!=belong[i-1])
        {
            for(int j=i;j<=n+1;j++)
                f[belong[i]][j]=max(f[belong[i]][j-1],solve(root[i-2],root[j],a[j],33)^a[j]);
        }
    }
    LL lastans=0;
    while(m--)
    {
        LL x,y;x=read();y=read();
        LL l=min(((x+lastans)%n)+1LL,((y+lastans)%n)+1LL);
        LL r=max(((x+lastans)%n)+1LL,((y+lastans)%n)+1LL);
        l++;r++;
        LL ans=solve(root[l-2],root[r],a[l-1],33)^a[l-1];
        for(int i=l;i<=r;i++)
        {
            if(i==2||belong[i]!=belong[i-1])
            {
                ans=max(ans,f[belong[i]][r]);
                break;
            }
            ans=max(ans,solve(root[l-2],root[r],(LL)a[i],33)^a[i]);
        }
        lastans=ans;
        printf("%lld\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值