QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#175928#6631. Maximum Bitwise ORRobeZHWA 135ms3660kbC++144.6kb2023-09-11 05:45:502023-09-11 05:45:50

Judging History

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

  • [2023-09-11 05:45:50]
  • 评测
  • 测评结果:WA
  • 用时:135ms
  • 内存:3660kb
  • [2023-09-11 05:45:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define subnb true
#define Lnb true
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define lson(x) 2*x+1
#define rson(x) 2*x+2

//const int N = (int)1e5 + 50;
const int N = (int)100 + 50;
const int INF = (int)1e9;

struct Tree {
    int dat[N * 4];
    void init(int n) {
        fill(dat, dat + 4 * n, -1);
    }
    void update(int pos, int x, int l, int r, int val) {
        if(l == r) {dat[x] = max(dat[x], val); return ;}
        int mid = (l + r) / 2;
        if(pos <= mid) update(pos, lson(x), l, mid, val);
        else update(pos, rson(x), mid + 1, r, val);
        dat[x] = max(dat[lson(x)], dat[rson(x)]);
    }
    int query(int a, int b, int x, int l, int r) {
        if(r < a || b < l) return -1;
        int mid = (l + r) / 2;
        if(a <= l && r <= b) return dat[x];
        return max(query(a, b, lson(x), l, mid), query(a, b, rson(x), mid + 1, r));
    }
} tree[30];

template<class T>
struct RMQ {
    vector<vector<T>> jmp;

    RMQ(const vector<T>& V) {
        int N = sz(V), on = 1, depth = 1;
        while (on < sz(V)) on *= 2, depth++;
        jmp.assign(depth, V);
        rep(i,0,depth-1) rep(j,0,N)
                jmp[i+1][j] = (jmp[i][j] | jmp[i][min(N - 1, j + (1 << i))]);
    }

    T query(int a, int b) {
        b++;
        if(a == b) return 0;
//        assert(a < b); // or return inf if a == b
        int dep = 31 - __builtin_clz(b - a);
        return jmp[dep][a] | jmp[dep][b - (1 << dep)];
    }
};


int n, q;
vi a;
struct Qr {
    int r, idx;
};
pii res[N];
vector<Qr> qrs[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    cin >> T;
    while(T--) {
        cin >> n >> q;
        a.resize(n);
        rep(i, 0, n) cin >> a[i];
        RMQ<int> rmq(a);

//        tree.init(0, 0, n - 1);
        rep(i, 0, 30) tree[i].init(n);
        rep(i, 0, n) qrs[i].clear();
        rep(i, 0, q) {
            int l, r;
            cin >> l >> r;
            l--, r--;
            qrs[l].push_back({r, i});
        }
        vi la(30, n);
        auto add_num = [&](int i) {
            int rb = 0;
            for (int lb = 0; lb < 30; lb = rb + 1) {
                rb = lb;
                while(rb < 30 && !(a[i] >> rb & 1)) rb++;
                if(rb >= 30) break;
                assert(a[i] >> rb & 1);
                tree[lb].update(i, 0, 0, n - 1, rb);
            }
        };
        vi in(n, 0);
        for (int i = n - 1; i >= 0; i--) {
            rep(b, 0, 30) {
                if(a[i] >> b & 1) {
                    if(la[b] != n) {
                        in[la[b]]--;
                        if(in[la[b]] == 0) add_num(la[b]);
                    }
                    la[b] = i;
                    in[i]++;
                }
            }

            for (auto &qr : qrs[i]) {

                int r = qr.r, idx = qr.idx;
//                node nd = tree.query(l, r, 0, 0, n - 1);
                int sum = rmq.query(i, r);
                int high = 0;
                rep(b, 0, 30) if (sum >> b & 1) high = b;
                int want = (1 << (high + 1)) - 1;
                if (sum == want) {
                    res[idx] = {want, 0};
                } else {
                    int lb = INF, rb = -1;
                    rep(j, 0, high + 1) {
                        if (!(sum >> j & 1)) lb = min(lb, j), rb = max(rb, j);
                    }
                    // want to cover [lb, rb];
                    bool found = false;
                    rep(b, 0, lb) {
                        found |= tree[b].query(i, r, 0, 0, n - 1) >= rb;
                    }

                    rep(b, 0, 30) {
                        int li = la[b];
                        if(li <= r) {
                            int osum = rmq.query(i, li - 1) | rmq.query(li + 1, r);
                            rep(k, 0, 30) {
                                if((1 << k) <= a[li]) found |= (osum | (a[li] ^ (a[li] - (1 << k)))) == want;
                            }
                        }
                    }
                    res[idx] = {want, found ? 1 : 2};
//                    cout << want << " ";
//                    if (found) cout << 1 << "\n";
//                    else cout << 2 << '\n';
                }
            }
        }
        rep(i, 0, q) cout << res[i].first << " " << res[i].second << "\n";
    }
}

详细

Test #1:

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

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: 132ms
memory: 3660kb

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: 0
Accepted
time: 132ms
memory: 3612kb

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:

ok 200000 numbers

Test #4:

score: -100
Wrong Answer
time: 135ms
memory: 3544kb

input:

33333
3 3
925088809 339284112 289540728
3 3
1 3
1 1
3 3
422399522 892365243 216341776
3 3
3 3
1 2
3 3
668932010 837523227 840095874
1 3
1 3
3 3
3 3
731584574 357877180 359063739
1 1
1 1
3 3
3 3
463358343 833924976 847087403
2 3
3 3
1 2
3 3
377154649 772000701 656357011
2 3
1 2
2 3
3 3
977492169 5540...

output:

536870911 2
1073741823 2
1073741823 2
268435455 2
268435455 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
536870911 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
67108863 2
1073741823 2
1073741...

result:

wrong answer 21332nd numbers differ - expected: '1', found: '2'