QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#189044#6631. Maximum Bitwise ORreal_sigma_team#WA 258ms3700kbC++203.5kb2023-09-26 19:56:242023-09-26 19:56:25

Judging History

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

  • [2023-09-26 19:56:25]
  • 评测
  • 测评结果:WA
  • 用时:258ms
  • 内存:3700kb
  • [2023-09-26 19:56:24]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

using ll = long long;

const int N = 3e5 + 5;

struct segtree {
    int R;
    vector<int> tr;

    segtree() = default;
    segtree(int n) {
        R = 1;
        while (R < n) R <<= 1;
        tr.assign(2 * R, -1);
    }

    void update(int k, int x) {
        k += R;
        tr[k] = x;
        while (k > 1) {
            k >>= 1;
            tr[k] = max(tr[k << 1], tr[k << 1 | 1]);
        }
    }

    int get(int l, int r) {
        int res = -1;
        for (l += R, r += R; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) res = max(res, tr[l++]);
            if (~r & 1) res = max(tr[r--], res);
        }
        return res;
    }
};

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<vector<int>> pref(n + 1, vector<int>(30));
    vector<vector<int>> pos(30, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        pref[i + 1] = pref[i];
        for (int j = 0; j < 30; ++j) {
            if (a[i] >> j & 1) {
                pref[i + 1][j]++;
                pos[j].push_back(i);
            }
        }
    }
    vector<vector<int>> nxt(n, vector<int>(30, -1));
    for (int i = 0; i < n; ++i) {
        if (a[i] == 0) continue;
        int cur = -1;
        for (int j = log2(a[i]); j >= 0; --j) {
            if (a[i] >> j & 1) cur = j;
            nxt[i][j] = cur;
        }
    }
    segtree trmx(n);
    for (int i = 0; i < n; ++i) {
        trmx.update(i, a[i]);
    }
    vector<segtree> trnxt(30, segtree(n));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 30; ++j) {
            trnxt[j].update(i, nxt[i][j]);
        }
    }
    while (q--) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        vector<int> cnt(30);
        for (int i = 0; i < 30; i++) {
            cnt[i] = pref[r + 1][i] - pref[l][i];
        }
        int vor = 0;
        for (int i = 0; i < 30; ++i) {
            if (cnt[i] > 0) vor |= 1 << i;
        }
        int v = trmx.get(l, r);
        for (int i = 0; 1 << i <= v; ++i) v |= 1 << i;
        cout << v << ' ';
        if (vor == v) {
            cout << "0";
        } else {
            int vl = 1e9, vr = -1;
            for (int i = 0; i < 30; ++i) {
                if (cnt[i] == 0) {
                    vl = min(vl, i);
                    vr = max(vr, i + 1);
                }
            }
            for (int i = 0; i < 30; ++i) {
                if (vl <= i && i <= vr) continue;
                if (cnt[i] == 1) {
                    int k = *lower_bound(all(pos[i]), l);
                    trnxt[vl].update(k, nxt[k][vl]);
                }
            }
            if (trnxt[vl].get(l, r) >= vr) {
                cout << "1";
            } else {
                cout << "2";
            }
            for (int i = 0; i < 30; ++i) {
                if (vl <= i && i <= vr) continue;
                if (cnt[i] == 1) {
                    int k = *lower_bound(all(pos[i]), l);
                    trnxt[vl].update(k, nxt[k][vl]);
                }
            }
        }
        cout << '\n';
    }
}

int main() {
#ifndef LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#else
    freopen("input.txt", "r", stdin);
#endif
    int t;
    cin >> t;
    while (t--)
    solve();
    return 0;
}

詳細信息

Test #1:

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

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: 258ms
memory: 3596kb

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: 186ms
memory: 3700kb

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'