QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213730#6631. Maximum Bitwise ORucup-team228#WA 151ms3772kbC++203.8kb2023-10-14 15:47:452023-10-14 15:47:46

Judging History

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

  • [2023-10-14 15:47:46]
  • 评测
  • 测评结果:WA
  • 用时:151ms
  • 内存:3772kb
  • [2023-10-14 15:47:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template <typename T>
struct segment_tree {
    static const int N = 1e5 + 10;
    const T inf = numeric_limits<T>::max() / 2;
    T t[2 * N + 10];
    void build() {
        for (int i = N - 1; i > 0; i--) {
            t[i] = t[i << 1] | t[i << 1 | 1];
        }
    }
    void update(int pos, T val) {
        for (t[pos += N] = val; pos > 1; pos >>= 1) {
            t[pos >> 1] = t[pos] | t[pos ^ 1];
        }
    }
    T get(int l, int r) {
        T res = 0;
        for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) res |= t[l++];
            if (!(r & 1)) res |= t[r--];
        }
        return res;
    }
};

template <typename T>
struct segment_tree_max {
    static const int N = 1e5 + 10;
    const T inf = numeric_limits<T>::max() / 2;
    T t[2 * N + 10];
    void build() {
        for (int i = N - 1; i > 0; i--) {
            t[i] = max(t[i << 1], t[i << 1 | 1]);
        }
    }
    void update(int pos, T val) {
        for (t[pos += N] = val; pos > 1; pos >>= 1) {
            t[pos >> 1] = max(t[pos], t[pos ^ 1]);
        }
    }
    T get(int l, int r) {
        T res = -inf;
        for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) res = max(res, t[l++]);
            if (!(r & 1)) res = max(res, t[r--]);
        }
        return res;
    }
};

segment_tree<int> tree;
segment_tree_max<int> tree_max;

const int B = 30;
const int N = 1e5 + 10;
int a[N];
vector<int> pos[B][B];

bool getbit(int mask, int bit) {
    return mask & (1 << bit);
}

pair<int, int> get(int mask) {
    int l = -1, r = -1;
    for (int i = 0; i < B; i++) {
        if (getbit(mask, i)) {
            if (l == -1) {
                l = i;
            }
            r = i;
        }
    }
    return {l, r};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < B; i++) {
            for (int j = 0; j < B; j++) {
                pos[i][j].clear();
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int b = 0; b < B && (1 << b) <= a[i]; b++) {
                int x = a[i] ^ (a[i] - (1 << b));
                auto [l, r] = get(x);
                pos[l][r].push_back(i);
            }
        }
        for (int i = 1; i <= n; i++) {
            tree.update(i, a[i]);
            tree_max.update(i, a[i]);
        }
        while (q--) {
            int l, r;
            cin >> l >> r;
            int zero_or = tree.get(l, r);
            int max_val = tree_max.get(l, r);
            auto [_, max_bit] = get(max_val);
            int ans = (1 << (max_bit + 1)) - 1;
            if (zero_or == ans) {
                cout << ans << " 0\n";
            } else {
                int trg = ans ^ zero_or;
                auto [lef, rig] = get(trg);
                bool ok = false;
                for (int L = 0; L <= lef; L++) {
                    for (int R = rig; R <= B - 1; R++) {
                        auto it = lower_bound(pos[L][R].begin(), pos[L][R].end(), l);
                        if (it != pos[L][R].end() && *it <= r) {
                            ok = true;
                        }
                    }
                }
                if (ok) {
                    cout << ans << " 1\n";
                } else {
                    cout << ans << " 2\n";
                }
            }
        }
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

详细

Test #1:

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

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: 151ms
memory: 3764kb

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: 133ms
memory: 3772kb

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'