Solution:CF620F(Xors on Segments)

· · 题解

Link:CF620F

首先我们看到显眼但不那么神奇的 n=5\times 10^4,m=5\times10^3,然后我们看到不那么显眼但非常神奇的时限 10\rm s。

于是考虑乱搞,我们发现问题很显然的可以转化为给 2 个数组,每个数组选一个数的最大异或和。然后我们就能想到 \mathrm O(nm\log v) 的 Trie。

然后可以发现过不去,乱搞一下,首先我们看到很多答案都是一样的,大部分询问的答案都等于区间 [1,n] 的答案 c。于是我们 \mathrm O(n^2) 的处理出离每个数 x 最近的 y,使得 f(a_x,a_y)=c。对于某次询问,如果 x,y 都在这个区间里直接输出 c 并退出即可。

然后就喜提 slowest solution。

#include <algorithm>
#include <iostream>
#include <array>

using namespace std;

struct trie_node
{
    int edtag;
    array<int,2> sons;
};

array<trie_node,1000001> trie;
array<int,50001> nums,srted,tmparr,maxvans;

int prexor(int target)
{
    switch(target&3)
    {
        case 0:
            return target;
        case 1:
            return 1;
        case 2:
            return target+1;
        default:
            return 0;
    }
}
int get_mxxor(int curnd,int depth,const int target)
{
    if(depth<0)
        return trie[curnd].edtag^target;
    int tmp=trie[curnd].sons[(target>>depth&1)^1];
    if(tmp)
        return get_mxxor(tmp,depth-1,target);
    return get_mxxor(trie[curnd].sons[target>>depth&1],depth-1,target);
}
inline int abs(int x)
{
    return x<0?-x:x;
}

int main(int argc,char* argv[],char* envp[])
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int cnt,cntq,maxv=0,cntnode=0,answer;
    cin>>cnt>>cntq;
    maxvans.fill(-1e9);
    for(int i=1;i<=cnt;i++)
        cin>>nums[i];
    for(int i=0;i<cnt;i++)
        srted[i]=nums[i+1];
    sort(srted.begin(),srted.begin()+cnt);
    for(int i=0;i<cnt;i++)
    {
        int curnode=0,tmp=prexor(srted[i]-1);
        for(int j=19;~j;j--)
        {
            if(!trie[curnode].sons[tmp>>j&1])
                trie[curnode].sons[tmp>>j&1]=++cntnode;
            curnode=trie[curnode].sons[tmp>>j&1];
        }
        trie[curnode].edtag=tmp;
        maxv=max(maxv,get_mxxor(0,19,prexor(srted[i])));
    }
    for(int i=0;i<=cntnode;i++)
        trie[i].sons.fill(0);
    for(int i=1;i<=cnt;i++)
        srted[i-1]=prexor(nums[i]),tmparr[i-1]=prexor(nums[i]-1);
    for(int i=0;i<cnt;i++)
        for(int j=0;j<cnt;j++)
            if(nums[i+1]>=nums[j+1]&&(srted[i]^tmparr[j])==maxv&&abs(i-j)<abs(maxvans[i]-i))
                maxvans[i+1]=j+1;
    while(cntq--)
    {
        int rgl,rgr;
        cntnode=0,answer=0;
        cin>>rgl>>rgr;
        for(int i=rgl;i<=rgr;i++)
        {
            srted[i-rgl]=nums[i];
            if(maxvans[i]>=rgl&&maxvans[i]<=rgr)
            {
                cout<<maxv<<'\n';
                goto nextcase;
            }
        }
        sort(srted.begin(),srted.begin()+rgr-rgl+1);
        for(int i=0;i<=rgr-rgl;i++)
        {
            int curnode=0,tmp=prexor(srted[i]-1);
            for(int j=19;~j;j--)
            {
                if(!trie[curnode].sons[tmp>>j&1])
                    trie[curnode].sons[tmp>>j&1]=++cntnode;
                curnode=trie[curnode].sons[tmp>>j&1];
            }
            trie[curnode].edtag=tmp;
            answer=max(answer,get_mxxor(0,19,prexor(srted[i])));
            if(answer==maxv)
                break;
        }
        for(int i=0;i<=cntnode;i++)
            trie[i].sons.fill(0);
        cout<<answer<<'\n';
        nextcase:;
    }
    return 0;
}