QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213849#6631. Maximum Bitwise ORucup-team228#WA 203ms3760kbC++207.0kb2023-10-14 16:21:452023-10-14 16:21:46

Judging History

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

  • [2023-10-14 16:21:46]
  • 评测
  • 测评结果:WA
  • 用时:203ms
  • 内存:3760kb
  • [2023-10-14 16:21: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_bit[B];
vector<int> pos[B][B];
bool ban[N];
int ban_pos[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};
}

void solve_precalc(int n) {
    for (int i = 1; i <= n; i++) {
        ban[i] = false;
    }
    for (int i = 0; i < B; i++) {
        pos_bit[i].clear();
        ban_pos[i] = 0;
    }
    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; b++) {
            if (getbit(a[i], b)) {
                pos_bit[b].push_back(a[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]);
    }
}

pair<int, int> solve_get(int l, int 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) {
        return {ans, 0};
    } else {
        int trg = ans ^ zero_or;
        auto [lef, rig] = get(trg);
        bool ok = false;

        for (int b = 0; b < B; b++) {
            ban_pos[b] = 0;
            auto it = lower_bound(pos_bit[b].begin(), pos_bit[b].end(), l);
            if (it != pos_bit[b].end() && *it <= r) {
                it++;
                if (it == pos_bit[b].end() || *it > r) {
                    it--;
                    ban_pos[b] = *it;
                }
            }
        }

        for (int L = 0; L <= lef && !ok; L++) {
            for (int i = rig; i <= B - 1; i++) {
                ban[ban_pos[i]] = true;
            }
            for (int R = rig; R <= B - 1; R++) {
                ban[ban_pos[R]] = false;
                auto it = lower_bound(pos[L][R].begin(), pos[L][R].end(), l);
                while (it != pos[L][R].end() && ban[*it]) {
                    it++;
                }
                if (it != pos[L][R].end() && *it <= r) {
                    ok = true;
                    break;
                }
            }
            ban[ban_pos[L]] = true;
        }
        for (int i = 0; i < B; i++) {
            ban[ban_pos[i]] = false;
        }

        if (ok) {
            return {ans, 1};
        } else {
            return {ans, 2};
        }
    }
}

pair<int, int> slow_get(int l, int r) {
    int tot_or = 0;
    for (int i = l; i <= r; i++) {
        tot_or |= a[i];
    }
    auto [_, max_bit] = get(tot_or);
    int res = (1 << (max_bit + 1)) - 1;
    if (tot_or == res) {
        return {res, 0};
    } else {
        for (int i = l; i <= r; i++) {
            for (int b = 0; b < B && (1 << b) <= a[i]; b++) {
                int x = a[i] ^ (a[i] - (1 << b));
                int cur = x;
                for (int j = l; j <= i - 1; j++) {
                    cur |= a[j];
                }
                for (int j = i + 1; j <= r; j++) {
                    cur |= a[i];
                }
                if (cur == res) {
                    return {res, 1};
                }
            }
        }
        return {res, 2};
    }
}

void stress() {
    mt19937 rnd;
    while (true) {
        int n = rnd() % 10 + 1;
        int q = rnd() % 100 + 1;
        for (int i = 1; i <= n; i++) {
            a[i] = rnd() % 20;
        }

        solve_precalc(n);

        int mem_l, mem_r;
        bool ok = true;
        for (int i = 1; i <= q; i++) {
            int l = rnd() % n + 1;
            int r = rnd() % n + 1;
            if (l > r) {
                swap(l, r);
            }
            auto [ans, cnt] = solve_get(l, r);
            auto [res, cnt2] = slow_get(l, r);
            if (ans != res && cnt != cnt2) {
                ok = false;
                mem_l = l;
                mem_r = r;
            }
        }
        if (ok) {
            cout << "OK" << endl;
        } else {
            cout << "WA\n";
            cout << "1\n";
            cout << n << " 1\n";
            for (int i = 1; i <= n; i++) {
                cout << a[i] << " ";
            }
            cout << "\n";
            cout << mem_l << " " << mem_r << "\n\n";
            auto [ans, cnt] = solve_get(mem_l, mem_r);
            auto [res, cnt2] = slow_get(mem_l, mem_r);
            cout << ans << " " << cnt << "\n\n";
            cout << res << " " << cnt2 << "\n";
            break;
        }
    }
    exit(0);
}

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

    // stress();

    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        solve_precalc(n);
        while (q--) {
            int l, r;
            cin >> l >> r;
            auto [ans, cnt] = solve_get(l, r);
            cout << ans << " " << cnt << "\n";
        }
    }

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

詳細信息

Test #1:

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

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: 203ms
memory: 3680kb

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: 179ms
memory: 3760kb

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'