QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376522 | #6631. Maximum Bitwise OR | fzx | WA | 346ms | 9840kb | C++14 | 4.9kb | 2024-04-04 11:15:49 | 2024-04-04 11:15:50 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define pb push_back
using namespace std;
const int INF=1e5+5;
int n,q,a[INF],d[35],vis[INF];
vector <int> e2[35];
struct Segment{
#define ll tl[id]
#define rr tr[id]
#define ls(x) x<<1
#define rs(x) x<<1|1
int tl[INF<<2],tr[INF<<2],sum[INF<<2][35];
void push_up(int id) {
for (int i=0;i<30;i++)
sum[id][i]=max(sum[ls(id)][i],sum[rs(id)][i]);
}
void build(int l,int r,int id) {
ll=l;rr=r;
if (ll==rr) {
int r=0;
for (int i=0;i<30;i++) {
if ((a[ll]>>i)&1) {sum[id][i]=-1;r=i;continue;}
while (r+1<30 && !((a[ll]>>(r+1))&1)) r++;
if ((a[ll]>>(r+1))&1) sum[id][i]=r;
else sum[id][i]=-1;
}
// if (l==6) cerr<<sum[id][1]<<" qwq?\n";
return ;
}
int Mid=(ll+rr)>>1;
build(ll,Mid,ls(id));
build(Mid+1,rr,rs(id));
push_up(id);
}
void add(int l,int r,int v,int id) {
if (l<=ll && rr<=r) {
if (v==1) {
for (int i=0;i<30;i++)
sum[id][i]=d[i];
}
else {
int r=0;
for (int i=0;i<30;i++) {
if ((a[ll]>>i)&1) {sum[id][i]=-1;r=i;continue;}
while (r+1<30 && !((a[ll]>>(r+1))&1)) r++;
if ((a[ll]>>(r+1))&1) sum[id][i]=r;
else sum[id][i]=-1;
}
}
return ;
}
int Mid=(ll+rr)>>1;
if (l<=Mid) add(l,r,v,ls(id));
if (Mid<r) add(l,r,v,rs(id));
push_up(id);
}
void query(int l,int r,int Max,int id) {
if (l<=ll && rr<=r) {
d[Max]=max(d[Max],sum[id][Max]);
return ;
}
int Mid=(ll+rr)>>1;
if (l>Mid) query(l,r,Max,rs(id));
else if (r<=Mid) query(l,r,Max,ls(id));
else {query(l,r,Max,ls(id)),query(l,r,Max,rs(id));}
}
#undef ll
#undef rr
#undef ls
#undef rs
}T1;
void solve() {
cin>>n>>q;
for (int i=0;i<=30;i++) e2[i].clear();
for (int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=n;i++) {
for (int j=0;j<30;j++)
if ((a[i]>>j)&1) e2[j].pb(i);
}
T1.build(1,n,1);
for (int i=1;i<=q;i++) {
int l=0,r=0;
cin>>l>>r;
int Min=30,Max=-1,fl=0,R=0;
for (int j=0;j<30;j++) {
int x=upper_bound(e2[j].begin(),e2[j].end(),r)-lower_bound(e2[j].begin(),e2[j].end(),l);
if (x) R=max(R,j);
}
for (int j=0;j<=R;j++) {
int x=upper_bound(e2[j].begin(),e2[j].end(),r)-lower_bound(e2[j].begin(),e2[j].end(),l);
if (!x) Min=min(Min,j),Max=max(Max,j);
if (x==1) vis[*lower_bound(e2[j].begin(),e2[j].end(),l)]++;
}
if (Min>R) {
cout<<((1ll<<(R+1))-1)<<" 0\n";
for (int j=0;j<=R;j++) {
int x=upper_bound(e2[j].begin(),e2[j].end(),r)-lower_bound(e2[j].begin(),e2[j].end(),l);
if (x==1) vis[*lower_bound(e2[j].begin(),e2[j].end(),l)]=0;
}
continue;
}
Max=min(Max,R);
cout<<((1ll<<(R+1))-1)<<" ";
for (int j=0;j<=R;j++) {
int x=upper_bound(e2[j].begin(),e2[j].end(),r)-lower_bound(e2[j].begin(),e2[j].end(),l);
if (x==1) {
int xx=*lower_bound(e2[j].begin(),e2[j].end(),l);
if (!vis[xx]) continue;
if (vis[xx]>1) {
// cerr<<xx<<" oawjroiqweq\n";
vis[xx]=0;
for (int p=0;p<30;p++) d[p]=-1;
T1.add(xx,xx,1,1);
}
else {
vis[xx]=0;
int L=j;
for (int p=j-1;p>=0;p--)
if (!((a[xx]>>p)&1)) L=p;
else break;
if (L<=Min && Max<=j) fl=1;
for (int p=0;p<30;p++) d[p]=0;
T1.add(xx,xx,1,1);
}
}
}
for (int p=0;p<30;p++) d[p]=-1;
T1.query(l,r,Min,1);
// cerr<<Min<<" "<<Max<<" "<<d[Min]<<" qwq?\n";
if (d[Min]>=0 && d[Min]>=Max-1) fl=1;
for (int j=0;j<30;j++) {
int x=upper_bound(e2[j].begin(),e2[j].end(),r)-lower_bound(e2[j].begin(),e2[j].end(),l);
if (x==1) {
int xx=*lower_bound(e2[j].begin(),e2[j].end(),l);
T1.add(xx,xx,2,1);
}
}
if (Min>Max) cout<<"0\n";
else if (fl) cout<<"1\n";
else cout<<"2\n";
}
}
signed main()
{
ios::sync_with_stdio(false);
int t=0;cin>>t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7760kb
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: 244ms
memory: 9840kb
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: 303ms
memory: 9812kb
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: 346ms
memory: 9744kb
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 5690th numbers differ - expected: '2', found: '1'