QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#376504#6631. Maximum Bitwise ORfzxWA 160ms20016kbC++144.0kb2024-04-04 10:51:452024-04-04 10:51:46

Judging History

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

  • [2024-04-04 10:51:46]
  • 评测
  • 测评结果:WA
  • 用时:160ms
  • 内存:20016kb
  • [2024-04-04 10:51:45]
  • 提交

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++;
                sum[id][i]=r;    
            }
            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++;
                    sum[id][i]=r;    
                }
            }
            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=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) Min=min(Min,j),Max=max(Max,j);
            if (x==1) vis[*lower_bound(e2[j].begin(),e2[j].end(),l)]++; 
            if (x) R=max(R,j);
        }
        if (Min>R) {cout<<((1ll<<(R+1))-1)<<" 0\n";continue;}
        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]>1) {
                    for (int p=0;p<30;p++) d[p]=0;
                    T1.add(xx,xx,1,1);
                }
                else {
                    int L=j;
                    for (int p=j-1;p>=0;p--) 
                        if (!((a[xx]>>p)&1)) L=p;
                    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]=0;
        T1.query(l,r,Min,1);
        if (d[Min]>=Max) 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;
}

详细

Test #1:

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

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: 160ms
memory: 20016kb

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
1073741823 2
1073741823 2
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1073741823 0
1...

result:

wrong answer 3rd numbers differ - expected: '268435455', found: '1073741823'