QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#135137#6631. Maximum Bitwise ORsalvator_noster#WA 56ms102068kbC++144.1kb2023-08-05 11:58:182023-08-05 11:58:19

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 11:58:19]
  • 评测
  • 测评结果:WA
  • 用时:56ms
  • 内存:102068kb
  • [2023-08-05 11:58:18]
  • 提交

answer

#include <bits/stdc++.h>
 
using namespace std;

const int MAX_N = 100000 + 5;
const int MAX_M = 32;

int N, M, Q, a[MAX_N], sor[1 << 18];
struct SegmentNode{
    int cnt, minv, idx;
    
    SegmentNode (int a = 0, int b = 0, int c = 0) : cnt(a), minv(b), idx(c) {}
    
    void init(int x, int k, int l) {
        cnt = x >> k & 1;
        if (!cnt) minv = k;
        else {
            if (k) {
                int tmp = x & (1 << k) - 1;
                if (!tmp) minv = 0;
                else minv = 32 - __builtin_clz(tmp);
            }else {
                minv = 0;
            }
        }
        idx = cnt ? l : 0;
    }
    
    inline SegmentNode operator + (const SegmentNode &x) const {
        return (SegmentNode){cnt + x.cnt, min(minv, x.minv), idx ^ x.idx};
    }
}node[MAX_M][1 << 18], b[32];

void segment_build1(int i, int l, int r) {
    if (l == r) {
        sor[i] = a[l];
        return ;
    }
    int mid = l + r >> 1;
    segment_build1(i << 1, l, mid);
    segment_build1(i << 1 | 1, mid + 1, r);
    sor[i] = sor[i << 1] | sor[i << 1 | 1];
}

int segment_query1(int i, int l, int r, int ql, int qr) {
    if (l < ql || r > qr) {
        int res = 0, mid = l + r >> 1;
        if (ql <= mid) res = segment_query1(i << 1, l, mid, ql, qr);
        if (mid < qr) res |= segment_query1(i << 1 | 1, mid + 1, r, ql, qr);
        return res;
    }else {
        return sor[i];
    }
}

void segment_build2(int k, int i, int l, int r) {
    if (l == r) {
        node[k][i].init(a[l], k, l);
        return ;
    }
    int mid = l + r >> 1;
    segment_build2(k, i << 1, l, mid);
    segment_build2(k, i << 1 | 1, mid + 1, r);
    node[k][i] = node[k][i << 1] + node[k][i << 1 | 1];
}

void segment_modify2(int k, int i, int l, int r, int x, int v) {
    if (l == r) {
        node[k][i].init(v, k, l);
        return ;
    }
    int mid = l + r >> 1;
    mid < x ? segment_modify2(k, i << 1 | 1, mid + 1, r, x, v) : segment_modify2(k, i << 1, l, mid, x, v);
    node[k][i] = node[k][i << 1] + node[k][i << 1 | 1];
}

SegmentNode segment_query2(int k, int i, int l, int r, int ql, int qr) {
    if (l < ql || r > qr) {
        int mid = l + r >> 1;
        SegmentNode res(0, 32, 0);
        if (ql <= mid) res = segment_query2(k, i << 1, l, mid, ql, qr);
        if (mid < qr) res = res + segment_query2(k, i << 1 | 1, mid + 1, r, ql, qr);
        return res;
    }else {
        return node[k][i];
    }
}

int main() {
    int T;
    scanf("%d", &T);
    while (T --) {
        scanf("%d%d", &N, &Q);
        M = 0;
        for (int i = 1; i <= N; i ++) {
            scanf("%d", a + i);
            if (a[i]) M = max(M, 31 - __builtin_clz(a[i]));
        }
        segment_build1(1, 1, N);
        for (int i = 0; i <= M; i ++) {
            segment_build2(i, 1, 1, N);
        }
        while (Q --) {
            int l, r;
            scanf("%d%d", &l, &r);
            int res = segment_query1(1, 1, N, l, r);
            for (int i = 0; i <= M; i ++) {
                b[i] = segment_query2(i, 1, 1, N, l, r);
            }
            int x, y;
            for (x = M; x >= 0 && b[x].cnt < 2; x --);
            if (x < 0) {
                printf("%d 0\n", res);
                continue;
            }
            res = res | (1 << x + 1) - 1;
            for (y = 0; y <= x && b[y].cnt; y ++);
            if (y > x) {
                printf("%d 0\n", res);
                continue;
            }
            for (int i = 0; i < y; i ++)
                if (b[i].cnt == 1) {
                    int id = b[i].idx;
                    if (a[id] >> x & 1) segment_modify2(x, 1, 1, N, id, 0);
                }
            int cur = segment_query2(x, 1, 1, N, l, r).minv;
            if (cur <= y) printf("%d 1\n", res);
            else printf("%d 2\n", res);
            for (int i = 0; i < y; i ++)
                if (b[i].cnt == 1) {
                    int id = b[i].idx;
                    if (a[id] >> x & 1) segment_modify2(x, 1, 1, N, id, a[id]);
                }
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 102068kb

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: 56ms
memory: 101968kb

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:

924704060 0
149840457 0
515267304 0
635378394 0
416239424 0
960156404 0
431278082 0
629009153 0
140374311 0
245014761 0
445512399 0
43894730 0
129731646 0
711065534 0
322643984 0
482420443 0
202661591 0
529979773 0
520572847 0
500838890 0
224446016 0
228171383 0
973333717 0
8493236 0
622547486 0
677...

result:

wrong answer 1st numbers differ - expected: '1073741823', found: '924704060'