QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#375416#6631. Maximum Bitwise ORsuisdavidWA 58ms12100kbC++143.3kb2024-04-03 10:24:392024-04-03 10:24:41

Judging History

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

  • [2024-04-03 10:24:41]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:12100kb
  • [2024-04-03 10:24:39]
  • 提交

answer

#include <iostream>
#include <vector>
using namespace std;

const int maxn=100005;
int T,n,q,L[maxn],R[maxn],a[maxn],nex[maxn][30],st[maxn][17],lg[maxn],f[30][30],g[30][30],p[maxn],tot,temp[maxn],cnt;
vector<int>pos[maxn];
pair<int,int>ans[maxn];
int read()
{
	int s=0;char c=getchar();
	while (c<'0'||c>'9')
	{
		c=getchar();
	}
	while (c>='0'&&c<='9')
	{
		s=s*10+c-'0';c=getchar();
	}
	return s;
}
void write(int s)
{
	if (s<10)
	{
		putchar(s%10+'0');return;
	}
	write(s/10);putchar(s%10+'0');
}

void _init()
{
	for (int j=1;j<17;j++)
	{
		for (int i=1;i+(1<<j)-1<=n;i++)
		{
			st[i][j]=(st[i][j-1]|st[i+(1<<(j-1))][j-1]);
		}
	}
}
int getor(int l,int r)
{
	if (l>r)
	{return 0;}
	return st[l][lg[r-l+1]]|st[r-(1<<lg[r-l+1])+1][lg[r-l+1]]; 
}
int main()
{
	int j=-1,k=1;
	for (int i=1;i<maxn;i++)
	{
		if (i==k)
		{
			k<<=1;j++;
		}
		lg[i]=j;
	}
	for (int i=0;i<30;i++)
	{
		for (int j=i;j<30;j++)
		{
			g[i][j]=(1<<(j+1))-(1<<i);
		}
	}
	T=read();
	while (T--)
	{
		n=read();q=read();tot=cnt=0;
		for (int i=0;i<30;i++)
		{
			for (int j=0;j<30;j++)
			{
				f[i][j]=0;
			}
		}
		for (int i=1;i<=n;i++)
		{
			pos[i].clear();
			a[i]=read();st[i][0]=a[i];
			for (int j=0,k=-1;j<30;j++)
			{
				if (a[i]&(1<<j))
				{
					nex[i][j]=k;k=-1;
				}
				else if (k==-1)
				{
					k=j;
				}
			}
		}
		_init();
		for (int i=1;i<=q;i++)
		{
			L[i]=read();R[i]=read();pos[R[i]].push_back(i);
		}
		for (int i=1;i<=n;i++)
		{
			temp[cnt=1]=i;
			for (int j=tot,x=a[i];j>=1;j--)
			{
				if ((a[p[j]]|x)>x)
				{
					x|=a[p[j]];temp[++cnt]=p[j];
				}
				else
				{
					int id=p[j]; 
					for (int k=0;k<30;k++)
					{
						if ((a[id]&(1<<k))&&nex[id][k]!=-1)
						{
							f[nex[id][k]][k]=max(f[nex[id][k]][k],id);
						}
					}
				}
			}
			tot=cnt;
			for (int i=1;i<=tot;i++)
			{
				p[i]=temp[tot-i+1];
			}
			int sz=pos[i].size();
			for (int j=0;j<sz;j++)
			{
				int id=pos[i][j],val=getor(L[id],i),len=0;ans[id].first=0;
				for (int k=29;k>=0;k--)
				{
					if (val&(1<<k))
					{
						ans[id].first=(1<<(k+1))-1;len=k;
						break;
					}
				}
				if (val==ans[id].first)
				{
					ans[id].second=0;
				}
				else
				{
					int mx=-1,mn=0,flg=0;
					for (int k=len;k>=0;k--)
					{
						if (!(val&(1<<k)))
						{
							if (mx==-1)
							{mx=k;}
							mn=k;
						}
					}
					for (int l=0;l<=mn;l++)
					{
						for (int r=mx;r<30;r++)
						{
							if (f[l][r]>=L[id])
							{
								flg=1;
								break;
							}
						}
					}	
					for (int k=1;k<=tot&&!flg;k++)
					{
						if (p[k]>=L[id])
						{
							int newid=p[k],vl=(getor(L[id],newid-1)|getor(newid+1,i));
							for (int x=0;x<29;x++)
							{
								if ((a[newid]&(1<<x))&&nex[newid][x]!=-1)
								{
									int pr=x,pl=nex[newid][x];
									if ((vl|g[pl][pr])==ans[id].first)
									{
										flg=1;break;
									}
								}
							}
						}
					}
					if (flg)
					{
						ans[id].second=1;
					}
					else
					{
						ans[id].second=2;
					}
				}
			}
		}
		for (int i=1;i<=q;i++)
		{
			write(ans[i].first);putchar(' ');write(ans[i].second);putchar('\n');
		}
	}
}
/*
1
3 2
10 10 5
1 2
1 3
*/

詳細信息

Test #1:

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

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: 0
Accepted
time: 53ms
memory: 10828kb

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:

ok 200000 numbers

Test #3:

score: 0
Accepted
time: 58ms
memory: 11868kb

input:

50000
2 2
924896435 917026400
1 2
1 2
2 2
322948517 499114106
1 2
2 2
2 2
152908571 242548777
1 1
1 2
2 2
636974385 763173214
1 2
1 1
2 2
164965132 862298613
1 1
1 2
2 2
315078033 401694789
1 2
1 2
2 2
961358343 969300127
2 2
1 2
2 2
500628228 28065329
1 2
1 2
2 2
862229381 863649944
1 2
2 2
2 2
541...

output:

1073741823 2
1073741823 2
536870911 2
536870911 2
268435455 2
268435455 2
1073741823 2
1073741823 2
268435455 2
1073741823 2
536870911 2
536870911 2
1073741823 2
1073741823 2
536870911 2
536870911 2
1073741823 2
1073741823 2
1073741823 2
268435455 2
536870911 2
536870911 2
1073741823 2
1073741823 2
...

result:

ok 200000 numbers

Test #4:

score: -100
Wrong Answer
time: 58ms
memory: 12100kb

input:

33333
3 3
925088809 339284112 289540728
3 3
1 3
1 1
3 3
422399522 892365243 216341776
3 3
3 3
1 2
3 3
668932010 837523227 840095874
1 3
1 3
3 3
3 3
731584574 357877180 359063739
1 1
1 1
3 3
3 3
463358343 833924976 847087403
2 3
3 3
1 2
3 3
377154649 772000701 656357011
2 3
1 2
2 3
3 3
977492169 5540...

output:

536870911 2
1073741823 2
1073741823 2
268435455 2
268435455 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
536870911 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
67108863 2
1073741823 2
1073741...

result:

wrong answer 45654th numbers differ - expected: '1', found: '2'