QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#149225#6631. Maximum Bitwise ORsyx2567TL 2ms7772kbC++142.0kb2023-08-24 09:23:582023-08-24 09:23:59

Judging History

你现在查看的是最新测评结果

  • [2023-08-24 09:23:59]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:7772kb
  • [2023-08-24 09:23:58]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=100005,M=400005,K=30;
int T,n,q,a[N];
int nxt[N][K+2];
int L[M],R[M];
int ma[M][K+2];//ma[i][j]:a[i],从j位向高位运行,所有到过都为0,最多到哪
void pushup(int x)
{
	for(int j=0;j<=K;j++) ma[x][j]=max(ma[x*2][j],ma[x*2+1][j]);
}
void build(int l,int r,int x)
{
	L[x]=l,R[x]=r;
	if(l==r)
	{
		int pre=-1;
		for(int j=K;j>=0;j--)
		{
			if(a[l]&(1<<j)) ma[x][j]=-1,pre=j-1;
			else ma[x][j]=pre;
		}
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,x*2);
	build(mid+1,r,x*2+1);
	pushup(x);
}
int find(int l,int r,int x,int w)
{
	if(l<=L[x]&&R[x]<=r) return ma[x][w];
	int mid=(L[x]+R[x])/2;
	if(r<=mid) return find(l,r,x*2,w);
	if(l>mid) return find(l,r,x*2+1,w);
	return max(find(l,r,x*2,w),find(l,r,x*2+1,w));
}
int typ[K+2],wl,wr;
int use[N],cnt;
bool check(int l,int r)
{
	use[++cnt]=l-1;
	use[++cnt]=r+1;
	sort(use+1,use+cnt+1);
	for(int i=1;i<cnt;i++)
	{
		int fl=use[i]+1,fr=use[i+1]-1;
		if(fl>fr) continue;
//		printf("%d %d %d:%d\n",fl,fr,wl,find(fl,fr,1,wl));
		if(find(fl,fr,1,wl)>=wr) return true;
	}
	return false;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&q);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int j=0;j<=K;j++) nxt[n+1][j]=n+1;
		for(int i=n;i>=1;i--)
			for(int j=0;j<=K;j++)
				if(a[i]&(1<<j)) nxt[i][j]=i;
				else nxt[i][j]=nxt[i+1][j];
		build(1,n,1);
		for(int i=1;i<=q;i++)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			int ma=-1;
			for(int j=0;j<=K;j++)
			{
				if(nxt[l][j]>r){typ[j]=0;continue;}
				ma=j;
				int now=nxt[l][j];
				if(nxt[now+1][j]<=r){typ[j]=2;continue;}
				typ[j]=1;use[++cnt]=now;
			}
//			for(int j=0;j<=K;j++) printf("%d ",typ[j]);
			printf("%d ",(1<<ma+1)-1);
			wl=wr=-1;
			for(int j=0;j<=ma;j++) if(typ[j]==0) wr=j;
			for(int j=ma;j>=0;j--) if(typ[j]==0) wl=j;
			if(wl==-1&&wr==-1) printf("0\n");
			else if(check(l,r)) printf("1\n");
			else printf("2\n");
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 7772kb

input:

1
3 2
10 10 5
1 2
1 3

output:

15 2
15 0

result:

ok 4 number(s): "15 2 15 0"

Test #2:

score: -100
Time Limit Exceeded

input:

100000
1 1
924704060
1 1
1 1
149840457
1 1
1 1
515267304
1 1
1 1
635378394
1 1
1 1
416239424
1 1
1 1
960156404
1 1
1 1
431278082
1 1
1 1
629009153
1 1
1 1
140374311
1 1
1 1
245014761
1 1
1 1
445512399
1 1
1 1
43894730
1 1
1 1
129731646
1 1
1 1
711065534
1 1
1 1
322643984
1 1
1 1
482420443
1 1
1 1
20...

output:

1073741823 2
268435455 2
536870911 2
1073741823 2
536870911 2
1073741823 2
536870911 2
1073741823 2
268435455 2
268435455 2
536870911 2
67108863 2
134217727 2
1073741823 2
536870911 2
536870911 2
268435455 2
536870911 2
536870911 2
536870911 2
268435455 2
268435455 2
1073741823 2
16777215 2
10737418...

result: