QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188993#6631. Maximum Bitwise ORucup-team004#WA 209ms32248kbC++203.5kb2023-09-26 18:37:552023-09-26 18:37:55

Judging History

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

  • [2023-09-26 18:37:55]
  • 评测
  • 测评结果:WA
  • 用时:209ms
  • 内存:32248kb
  • [2023-09-26 18:37:55]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 1 << 18;

int mx[30][N];

void build(int d, int p, int l, int r, const std::vector<int> &v) {
    if (r - l == 1) {
        mx[d][p] = v[l];
        return;
    }
    int m = (l + r) / 2;
    build(d, 2 * p, l, m, v);
    build(d, 2 * p + 1, m, r, v);
    mx[d][p] = std::max(mx[d][2 * p], mx[d][2 * p + 1]);
}

int query(int d, int p, int l, int r, int x, int y) {
    if (l >= y || r <= x) {
        return -1;
    }
    if (l >= x && r <= y) {
        return mx[d][p];
    }
    int m = (l + r) / 2;
    return std::max(query(d, 2 * p, l, m, x, y), query(d, 2 * p + 1, m, r, x, y));
}

void solve() {
    int N, Q;
    std::cin >> N >> Q;
    
    std::vector<int> A(N);
    for (int i = 0; i < N; i++) {
        std::cin >> A[i];
    }
    
    std::vector nxt(N + 1, std::vector<int>(30, N));
    for (int i = N - 1; i >= 0; i--) {
        nxt[i] = nxt[i + 1];
        for (int j = 0; j < 30; j++) {
            if (A[i] >> j & 1) {
                nxt[i][j] = i;
            }
        }
    }
    
    std::vector lst(N + 1, std::vector<int>(30, 0));
    for (int i = 0; i < N; i++) {
        lst[i + 1] = lst[i];
        for (int j = 0; j < 30; j++) {
            if (A[i] >> j & 1) {
                lst[i + 1][j] = i + 1;
            }
        }
    }
    std::vector<int> val(N, -1);
    for (int j = 29; j >= 0; j--) {
        for (int i = 0; i < N; i++) {
            if (A[i] >> j & 1) {
                val[i] = j;
            }
        }
        build(j, 1, 0, N, val);
    }
    
    for (int i = 0; i < Q; i++) {
        int L, R;
        std::cin >> L >> R;
        L--;
        
        int t = 29;
        while (t >= 0 && nxt[L][t] >= R) {
            t--;
        }
        int ans = (1 << (t + 1)) - 1;
        
        bool zero = true;
        for (int j = 0; j <= t; j++) {
            if (nxt[L][j] >= R) {
                zero = false;
            }
        }
        int step = 2;
        if (zero) {
            step = 0;
        } else {
            std::vector<std::array<int, 3>> u;
            int pre = 0, suf = 0;
            int t = L;
            for (int j = 0; j < 30; j++) {
                if (nxt[L][j] < R) {
                    u.push_back({nxt[L][j], j, 0});
                }
            }
            for (int j = 0; j < 30; j++) {
                if (lst[R][j] > L) {
                    u.push_back({lst[R][j], j, 1});
                    suf |= 1 << j;
                }
            }
            std::sort(u.begin(), u.end());
            auto work = [&](int l, int r) {
                if (l == r) {
                    return;
                }
                int v = (pre | suf) ^ ans;
                assert(v > 0);
                int x = __builtin_ctz(v);
                int y = std::__lg(v);
                if (query(x, 1, 0, N, l, r) >= y) {
                    step = 1;
                }
            };
            for (auto [i, j, type] : u) {
                work(t, i);
                t = i;
                if (type == 0) {
                    pre ^= 1 << j;
                } else {
                    suf ^= 1 << j;
                }
            }
            work(t, R);
        }
        
        std::cout << ans << " " << step << "\n";
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 32248kb

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: 185ms
memory: 32136kb

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

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:

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