QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#205730 | #6631. Maximum Bitwise OR | Geospiza# | WA | 195ms | 169728kb | C++20 | 3.4kb | 2023-10-07 17:10:39 | 2023-10-07 17:10:40 |
Judging History
answer
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define per(i,a,b) for(ll i=a;i>=b;--i)
typedef pair<ll,ll> pll;
const ll N=4e5+5;
ll n,m;
ll a[N];
ll nxt[32][N],cnt[32][N],pre[32][N];
struct Seg
{
ll tr[N<<2];
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define mid ((l+r)>>1)
void pushup(ll p)
{
tr[p]=max(tr[ls(p)],tr[rs(p)]);
return ;
}
void build(ll p,ll l,ll r,ll i)
{
if(l==r)
{
tr[p]=nxt[i][l];
return ;
}
build(ls(p),l,mid,i);
build(rs(p),mid+1,r,i);
pushup(p);
}
ll query(ll p,ll l,ll r,ll ql,ll qr)
{
if(l>qr||r<ql)return 0;
if(l>=ql&&r<=qr)return tr[p];
return query(ls(p),l,mid,ql,qr)+query(rs(p),mid+1,r,ql,qr);
}
}T[32];
pll check(ll l,ll r)
{
ll res=0,mx=-1;
vector<ll>v;
rep(i,0,30)if(cnt[i][r]-cnt[i][l-1]!=0)
{
res|=1LL<<i;
if(cnt[i][r]-cnt[i][l-1]==1)v.pb(pre[i][r]);
mx=max(mx,i);
}
sort(all(v));
unique(all(v));
ll kl=-1,kr=-1,now=0,rr=res;
while(rr)
{
if(rr%2==0)
{
if(!~kl)kl=now;
kr=now;
}
++now;
rr/=2;
}
if(!~kl)return (make_pair)((1LL<<(mx+1))-1,0LL);
ll lst=l;
for(auto x:v)
{
if(x-1>=lst)
{
if(T[kl].query(1,1,n,lst,x-1)>=kr)return (make_pair)((1<<(mx+1))-1,1LL);
}
ll rm=0,mxx=-1;
rep(i,0,30)if(cnt[i][r]-cnt[i][x]+cnt[i][x-1]-cnt[i][l-1]>0)
{
rm|=1LL<<i;
mxx=max(mxx,i);
}
ll kll=-1,krr=-1,noww=0,rrr=rm;
while(rrr)
{
if(rrr%2==0)
{
if(!~kll)kll=noww;
krr=noww;
}
++noww;
rrr/=2;
}
if(mx==mxx)
{
if(nxt[kll][x]>=krr)return (make_pair)((1<<(mx+1))-1,1LL);
}
else
{
if(!~kll)
{
if(nxt[mxx+1][x]>=max(mx,krr))return (make_pair)((1<<(mx+1))-1,1LL);
}
else if(nxt[kll][x]>=max(mx,krr))return (make_pair)((1<<(mx+1))-1,1LL);
}
lst=x+1;
}
if(r>=lst&&T[kl].query(1,1,n,lst,r)>=kr)return (make_pair)((1<<(mx+1))-1,1LL);
return (make_pair)((1<<(mx+1))-1,2LL);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
cin>>t;
while(t--)
{
cin>>n>>m;
rep(i,1,n)
{
cin>>a[i];
ll lst=-1;
per(j,30,0)
{
if((a[i]>>j)&1)nxt[j][i]=-1,lst=j;
else nxt[j][i]=lst;
}
}
rep(i,0,30)T[i].build(1,1,n,i);
rep(i,1,n)rep(j,0,30)
{
if((a[i]>>j)&1)cnt[j][i]=cnt[j][i-1]+1,pre[j][i]=i;
else cnt[j][i]=cnt[j][i-1],pre[j][i]=pre[j][i-1];
}
rep(i,1,m)
{
ll l,r;
cin>>l>>r;
auto ans=check(l,r);
cout<<ans.first<<" "<<ans.second<<"\n";
}
}
}
/*
1
3 2
10 10 5
1 2
1 3
3 2
10 10 5
1 2
1 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 135180kb
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: 127ms
memory: 169728kb
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: 180ms
memory: 161532kb
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: 195ms
memory: 163596kb
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 16168th numbers differ - expected: '2', found: '1'