跳到正文

题解

Solution:CF620F(Xors on Segments)

Argon_Cube约 6 分钟
原文
文章信息与版本

文章信息

发布
2022年10月18日
收录
2026年9月17日
更新
2026年11月9日
篇幅
约 6 分钟
获赞
1
题目
CF620F

版本

  1. v1当前
  • 【题目链接】

Link:CF620F

  • 【解题思路】

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

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

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

然后就喜提 slowest solution。

  • 【代码实现】
C++
#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;
}
原文