QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286753#6631. Maximum Bitwise ORjrjyyWA 302ms3856kbC++203.4kb2023-12-18 15:55:492023-12-18 15:55:51

Judging History

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

  • [2023-12-18 15:55:51]
  • 评测
  • 测评结果:WA
  • 用时:302ms
  • 内存:3856kb
  • [2023-12-18 15:55:49]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template <typename T, typename Cmp = std::less<T>>
struct RMQ {
    const Cmp cmp{};
    int n;
    std::vector<std::vector<T>> a;
    RMQ() = default;
    RMQ(const std::vector<T> &v) {
        init(v);
    }
    RMQ(const std::vector<T> &v, const Cmp &cmp_) : cmp{cmp_} {
        init(v);
    }
    void init(const std::vector<T> &ini) {
        n = int(ini.size());
        int lg = std::__lg(n);
        a.assign(lg + 1, std::vector<T>(n));
        a[0] = ini;
        for (int i = 0; i < lg; ++i) {
            for (int j = 0; j <= n - (2 << i); ++j) {
                a[i + 1][j] = std::min(a[i][j], a[i][j + (1 << i)], cmp);
            }
        }
    }
    T operator()(int l, int r) const {
        int k = std::__lg(r - l);
        return std::min(a[k][l], a[k][r - (1 << k)], cmp);
    }
};

constexpr int K = 30;

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<std::array<int, K>> pre(n + 1), suf(n + 1);
    pre[0].fill(-1);
    for (int i = 0; i < n; ++i) {
        pre[i + 1] = pre[i];
        for (int x = 0; x < 30; ++x) {
            if (a[i] >> x & 1) {
                pre[i + 1][x] = i;
            }
        }
    }
    suf[n].fill(n);
    for (int i = n - 1; i >= 0; --i) {
        suf[i] = suf[i + 1];
        for (int x = 0; x < 30; ++x) {
            if (a[i] >> x & 1) {
                suf[i][x] = i;
            }
        }
    }

    std::vector<int> v(n, -1);
    std::array<RMQ<int, std::greater<>>, K> rmq;
    for (int x = K - 1; x >= 0; --x) {
        for (int i = 0; i < n; ++i) {
            if (a[i] >> x & 1) {
                v[i] = x;
            }
        }
        rmq[x].init(v);
    }

    while (q--) {
        int l, r;
        std::cin >> l >> r;
        --l;

        int tot = 0, ans = 0;
        for (int x = 0; x < K; ++x) {
            if (pre[r][x] >= l) {
                tot |= 1 << x;
                ans = (2 << x) - 1;
            }
        }

        int step = 2;
        if (tot == ans) {
            step = 0;
        } else {
            std::vector<std::tuple<int, int, int>> pos{{r, -1, -1}};
            int vp = 0, vs = tot;
            for (int x = 0; x < K; ++x) {
                if (suf[l][x] < r) {
                    pos.emplace_back(suf[l][x], x, 1);
                }
                if (pre[r][x] >= l) {
                    pos.emplace_back(pre[r][x] - 1, x, -1);
                }
            }
            std::sort(pos.begin(), pos.end());
            int lst = l;
            for (auto [nxt, v, o] : pos) {
                int cur = (vp | vs) ^ ans;
                int lo = __builtin_ctz(cur), hi = std::__lg(cur);
                if (lst < nxt && rmq[lo](lst, nxt) >= hi) {
                    step = 1;
                    break;
                }
                lst = nxt;
                if (o == 1) {
                    vp ^= 1 << v;
                } else if (v != -1) {
                    vs ^= 1 << v;
                }
            }
        }

        std::cout << ans << " " << step << "\n";
    }
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    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: 1ms
memory: 3800kb

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: 302ms
memory: 3856kb

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: 300ms
memory: 3620kb

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'