QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#122671#6631. Maximum Bitwise ORinstallb#WA 174ms83696kbC++142.7kb2023-07-10 21:51:452023-07-10 21:51:47

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-10 21:51:47]
  • 评测
  • 测评结果:WA
  • 用时:174ms
  • 内存:83696kb
  • [2023-07-10 21:51:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define M 100005
int n,q,A[M];
using pii = pair<int,int>;
pii Ask(int x){
    int l=-1,r=-1,i=0;
    while(x){
        if(x&1);
        else {
            if(l==-1)l = i;
            r=i;
        }
        i++;
        x>>=1;
    }
    return pii(l,r);
}
int nex[30][M];
struct Seg{
    struct TNODE{
        int l,r,mx;
    }tree[M<<2];
    #define fa tree[p]
    #define lson tree[p<<1]
    #define rson tree[p<<1|1]
    void build(int l,int r,int *A,int p=1){
        fa.l=l,fa.r=r;
        if(l==r)return void(fa.mx = A[l]);
        int mid=l+r>>1;
        build(l,mid,A,p<<1);
        build(mid+1,r,A,p<<1|1);
        fa.mx=max(lson.mx,rson.mx);
    }
    int Query(int l,int r,int p=1){
        if(l==fa.l&&r==fa.r)return fa.mx;
        int mid=fa.l+fa.r>>1;
        if(r<=mid)return Query(l,r,p<<1);
        else if(l>mid)return Query(l,r,p<<1|1);
        else return max(Query(l,mid,p<<1),Query(mid+1,r,p<<1|1));
    }
    #undef fa
    #undef lson
    #undef rson
}T[30];
int cnt[30][M],pre[30][M];
pii solve(int l,int r){
    int x=0,mx=-1;
    vector<int>pl;
    for(int i=0;i<30;i++){
        if(cnt[i][r]-cnt[i][l-1]==0)continue;
        x|=1<<i;
        if(cnt[i][r]-cnt[i][l-1]==1)pl.push_back(pre[i][r]);
        mx = i;
    }
    sort(pl.begin(),pl.end());
    unique(pl.begin(),pl.end());
    auto [bl,br] = Ask(x);
    if(bl==-1) return pii((1<<mx+1)-1,0);
    int last = l;
    for(int &p_pl:pl){
        if(p_pl-1 >= last){
            if(T[bl].Query(last,p_pl-1) >= br) return pii((1<<mx+1)-1,1);
        }
        int res = 0;
        for(int i=0;i<30;i++)if(cnt[i][r]-cnt[i][p_pl]+cnt[p_pl-1]-cnt[l]>0)
            res |= 1<<i;
        auto [bl2,br2] = Ask(res);
        if(nex[bl2][p_pl]>=br2) return pii((1<<mx+1)-1,1);
        last = p_pl+1;
    }
    return pii((1<<mx+1)-1,2);
}
int main(){
    int Cas;cin>>Cas;
    while(Cas--){
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++){
            scanf("%d",&A[i]);
            int last = -1;
            for(int j=29;~j;--j){
                if((1<<j)&A[i])nex[j][i] = -1,last=j;
                else nex[j][i] = last;
            }
        }
        for(int i=0;i<30;i++)T[i].build(1,n,nex[i],1);
        for(int i=1;i<=n;i++){
            for(int j=0;j<30;j++)
                if((1<<j)&A[i])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];
        }
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            auto [x,y] = solve(l,r);
            printf("%d %d\n",x,y);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 79668kb

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: -100
Wrong Answer
time: 174ms
memory: 83696kb

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 1
268435455 1
536870911 1
1073741823 1
536870911 1
1073741823 1
536870911 1
1073741823 1
268435455 1
268435455 1
536870911 1
67108863 1
134217727 1
1073741823 1
536870911 1
536870911 1
268435455 1
536870911 1
536870911 1
536870911 1
268435455 1
268435455 1
1073741823 1
16777215 1
10737418...

result:

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