QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#376032 | #6631. Maximum Bitwise OR | schrodingerstom | WA | 99ms | 4008kb | C++14 | 4.2kb | 2024-04-03 19:53:55 | 2024-04-03 19:53:57 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
bool memBeg;
template<typename T,typename TT> void chkmin(T &x,TT y) {if(x>y) x=y;}
template<typename T,typename TT> void chkmax(T &x,TT y) {if(x<y) x=y;}
constexpr int mod=998244353;
void inc(int &x,int y) {x+=y; x-=(x>=mod)*mod;}
void dec(int &x,int y) {x-=y; x+=(x<0)*mod;}
constexpr int add(int x,int y) {return (x+y>=mod)?x+y-mod:x+y;}
constexpr int sub(int x,int y) {return (x<y)?x-y+mod:x-y;}
constexpr int quick_pow(int x,ll times,int ret=1) {
for(;times;times>>=1,x=1ll*x*x%mod) if(times&1) ret=1ll*ret*x%mod;
return ret;
}
constexpr int D=30;
constexpr int maxD=D+5;
constexpr int maxn=1e5+5;
int n,Q,a[maxn];
struct info1 {
int pos[maxD];
int operator[](int x) const {return pos[x];}
int& operator[](int x) {return pos[x];}
info1 operator+(const info1 &o) const {
info1 ret;
for(int i=0;i<D;i++) {
if(pos[i]==-1||o[i]==-1) ret[i]=-1;
else if(pos[i]&&o[i]&&pos[i]!=o[i]) ret[i]=-1;
else ret[i]=pos[i]|o[i];
}
return ret;
}
};
struct info2 {
int mx[maxD];
int operator[](int x) const {return mx[x];}
int& operator[](int x) {return mx[x];}
info2 operator+(const info2 &o) const {
info2 ret;
for(int i=0;i<D;i++) ret[i]=max(mx[i],o[i]);
return ret;
}
};
struct info {
int Or;
info1 p;
info2 q;
info operator+(const info &o) const {
info ret; ret.Or=Or|o.Or;
ret.p=p+o.p; ret.q=q+o.q;
auto flush=[&](int idx)->void {
if(!a[idx]) return;
for(int t=__lg(a[idx]);t>=0;t--) {
chkmax(ret.q[t],a[idx]^(a[idx]-(1<<t)));
}
};
for(int i=0;i<D;i++) {
if(ret.p[i]!=p[i]) flush(p[i]);
if(ret.p[i]!=o.p[i]) flush(o.p[i]);
}
return ret;
}
};
struct segment_tree {
info tree[maxn<<2];
void push_up(int cur) {
tree[cur]=tree[cur<<1]+tree[cur<<1|1];
}
void build(int tl,int tr,int cur) {
if(tl==tr) {
for(int i=0;i<D;i++) {
if(a[cur]&(1<<i)) tree[cur].p[i]=tl;
else tree[cur].p[i]=0;
tree[cur].q[i]=0;
}
tree[cur].Or=a[tl]; return;
}
int mid=(tl+tr)>>1;
build(tl,mid,cur<<1);
build(mid+1,tr,cur<<1|1);
push_up(cur);
}
info query(int tl,int tr,int cur,int l,int r) {
if(l<=tl&&tr<=r) return tree[cur];
int mid=(tl+tr)>>1;
if(l>mid) return query(mid+1,tr,cur<<1|1,l,r);
else if(r<=mid) return query(tl,mid,cur<<1,l,r);
else return query(tl,mid,cur<<1,l,r)+
query(mid+1,tr,cur<<1|1,l,r);
}
}T;
void mian() {
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
T.build(1,n,1);
while(Q--) {
int l,r;
scanf("%d%d",&l,&r);
static info tmp;
tmp=T.query(1,n,1,l,r);
if(!tmp.Or) {
printf("0 0\n"); continue;
}
int mx=(1<<(__lg(tmp.Or)+1))-1;
if(tmp.Or==mx) {
printf("%d %d\n",mx,0); continue;
}
bool one=false;
for(int i=0;i<D&&!one;i++) {
if((tmp.Or|tmp.q[i])==mx) {
one=true; break;
}
}
for(int i=0;i<D&&!one;i++) {
if(!tmp.p[i]) continue;
int idx=tmp.p[i],must=0;
for(int j=0;j<D;j++) {
if(tmp.p[j]==idx) must|=1<<j;
}
if(must!=(1<<i)) continue;
for(int j=0;j<D;j++) {
if((1<<j)>a[idx]) break;
int upd=a[idx]^(a[idx]-(1<<j));
if(!(upd&must)) continue;
if((tmp.Or|upd)==mx) {
one=true; break;
}
}
}
printf("%d %d\n",mx,one?1:2);
}
}
bool memEn;
void fl() {
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
int main() {
fprintf(stderr,"%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
// fl();
int _; scanf("%d",&_);
while(_--) mian();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3944kb
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: 99ms
memory: 3940kb
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: 86ms
memory: 3936kb
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: 81ms
memory: 4008kb
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 5770th numbers differ - expected: '1', found: '2'