QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181901 | #6631. Maximum Bitwise OR | ucup-team134 | WA | 0ms | 7996kb | C++14 | 2.3kb | 2023-09-17 03:29:47 | 2023-09-17 03:29:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=100050;
const int LOG=30;
int a[N],last[N][LOG];
int cnt[N];
int b[N];
const int M=2*LOG*N;
int root[LOG],ls[M],rs[M],mx[M],tsz;
int bld[N][LOG];
void Build(int&c,int ss,int se,int lg){
c=++tsz;
if(ss==se){
mx[c]=bld[ss][lg];
return;
}
int mid=ss+se>>1;
Build(ls[c],ss,mid,lg);
Build(rs[c],mid+1,se,lg);
mx[c]=max(mx[ls[c]],mx[rs[c]]);
}
int Get(int c,int ss,int se,int qs,int qe){
if(qs>qe||qs>se||ss>qe)return -1;
if(qs<=ss&&qe>=se)return mx[c];
int mid=ss+se>>1;
return max(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int main(){
int t;
scanf("%i",&t);
while(t--){
int n,q;
scanf("%i %i",&n,&q);
for(int i=1;i<=n;i++){
scanf("%i",&a[i]);
for(int j=0;j<LOG;j++){
if(a[i]>>j&1){
last[i][j]=i;
}else{
last[i][j]=last[i-1][j];
}
}
int one=-1;
for(int j=LOG-1;j>=0;j--){
if(a[i]>>j&1){
one=j;
}
if(one!=-1){
bld[i][j]=one;
}else{
bld[i][j]=-1;
}
}
}
for(int i=0;i<LOG;i++){
Build(root[i],1,n,i);
}
while(q--){
int l,r;
scanf("%i %i",&l,&r);
int OR=0,mx=0;
for(int i=0;i<LOG;i++){
if(last[r][i]>=l){
OR|=(1<<i);
mx=(1<<(i+1))-1;
}
}
int ans=0;
if(OR!=mx){
ans=2;
int L=0;
while(last[r][L]>=l)L++;
int R=L-1;
while(last[r][R]<l)R--;
while(last[r][R]>=l)R--;
vector<int> clr;
for(int i=0;i<LOG;i++){
if(last[r][i]>=l){
int spec=last[r][i];
if(last[spec][i]>=l)continue;
cnt[spec]++;
b[spec]=i;
clr.pb(spec);
}
}
sort(clr.begin(),clr.end());
int pre=l;
int mxr=-1;
for(int x:clr){
if(cnt[x]==1){
bool ok=true;
for(int i=L;i<R;i++){
if(a[x]>>i&1){
ok=false;
break;
}
}
bool good=false;
for(int i=R;i<LOG;i++){
if(a[x]>>i&1){
if(i==b[x]){
good=true;
}
break;
}
}
if(!good)ok=false;
if(ok)ans=1;
}
mxr=max(mxr,Get(root[L],1,n,pre,x-1));
pre=x+1;
cnt[x]=0;
}
if(mxr>=R)ans=1;
}
printf("%i %i\n",mx,ans);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7996kb
input:
1 3 2 10 10 5 1 2 1 3
output:
15 1 15 0
result:
wrong answer 2nd numbers differ - expected: '2', found: '1'