QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#500832#6631. Maximum Bitwise ORhcywoiWA 237ms3776kbC++203.4kb2024-08-01 21:24:252024-08-01 21:24:25

Judging History

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

  • [2024-08-01 21:24:25]
  • 评测
  • 测评结果:WA
  • 用时:237ms
  • 内存:3776kb
  • [2024-08-01 21:24:25]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 30;

int id[N][N], cur;

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];
    }

    const int M = 30;
    std::vector f0(n + 1, std::vector<int>(M, n));
    std::vector f1(n + 1, std::vector(M * (M + 1) / 2, n));
    for (int i = n - 1; i >= 0; i -- ) {
        for (int j = 0; j < M; j ++ ) {
            if (a[i] >> j & 1) {
                f0[i][j] = i;
            } else {
                f0[i][j] = f0[i + 1][j];
            }
        }

        for (int j = 0; j < M; j ++ ) {
            for (int k = i; k < M; k ++ ) {
                f1[i][id[j][k]] = f1[i + 1][id[j][k]];
            }
        }
        int lst = -1;
        for (int j = 0; j < M; j ++ ) {
            if (a[i] >> j & 1) {
                for (int x = lst + 1; x <= j; x ++ ) {
                    for (int y = x; y <= j; y ++ ) {
                        f1[i][id[x][y]] = i;
                    }
                }
                lst = j;
            }
        }
    }

    while (q -- ) {
        int l, r;
        std::cin >> l >> r;
        l --, r -- ;
        int t = -1;
        for (int i = M - 1; i >= 0; i -- ) {
            if (f0[l][i] <= r) {
                t = i;
                break;
            }
        }

        std::cout << (1 << t + 1) - 1 << " ";

        std::vector<int> pos;
        pos.push_back(l - 1);
        pos.push_back(r + 1);

        int os = 0;
        int sl = M, sr = -1;
        for (int i = 0; i <= t; i ++ ) {
            if (f0[l][i] <= r) {
                os |= 1 << i;
                if (f0[f0[l][i] + 1][i] > r) {
                    pos.push_back(f0[l][i]);
                }
            } else {
                sl = std::min(sl, i);
                sr = std::max(sr, i);
            }
        }
        if (sr == -1) {
            std::cout << 0 << "\n";
            continue;
        }
        std::sort(pos.begin(), pos.end());

        bool ok = 1;
        for (int i = 1; i < pos.size(); i ++ ) {
            int L = pos[i - 1], R = pos[i];
            L ++ ;
            R -- ;
            if (L > R) {
                continue;
            }
            if (f1[L][id[sl][sr]] <= R) {
                ok = 0;
                break;
            }
        }
        for (auto i : pos) {
            if (i < l || i > r) {
                continue;
            }

            int nos = os;
            for (int j = 0; j < M; j ++ ) {
                if (f0[l][j] == i && f0[i + 1][j] > r) {
                    nos ^= 1 << j;
                }
            }
            for (int j = 0; j < M; j ++ ) {
                if ((1 << j) > a[i]) {
                    break;
                }
                if ((nos | (a[i] ^ (a[i] - (1 << j)))) == (1 << t + 1) - 1) {
                    ok = 0;
                    break;
                }
            }
        }
        if (!ok) {
            std::cout << 1 << "\n";
        } else {
            std::cout << 2 << "\n";
        }
    }
}

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

    for (int i = 0; i < N; i ++ ) {
        for (int j = i; j < N; j ++ ) {
            id[i][j] = cur ++ ;
        }
    }

    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: 0ms
memory: 3504kb

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: 234ms
memory: 3776kb

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: 237ms
memory: 3552kb

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: 0
Accepted
time: 236ms
memory: 3644kb

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:

ok 199998 numbers

Test #5:

score: 0
Accepted
time: 224ms
memory: 3564kb

input:

20000
5 5
925473558 183799537 561353092 858424993 765513347
2 4
1 1
1 2
3 5
1 4
5 5
141075166 375934371 754066286 663820319 906342255
3 5
1 1
4 5
1 4
2 3
5 5
417114008 270241961 121113861 381529080 772448388
1 3
1 1
2 5
5 5
2 2
5 5
668136057 138968211 856562545 187058570 699131353
4 5
3 4
5 5
2 4
3 ...

output:

1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
268435455 2
1073741823 2
1073741823 2
1073741823 2
536870911 2
536870911 2
1073741823 2
1073741823 2
536870911 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
1073741823 2
268435455 2
107374...

result:

ok 200000 numbers

Test #6:

score: -100
Wrong Answer
time: 185ms
memory: 3640kb

input:

10000
10 10
464850425 447664857 363029101 653457810 349421643 98326037 214053360 578140626 808807764 145448013
7 9
9 10
8 10
3 7
9 10
5 8
3 3
4 5
5 9
5 6
10 10
164710533 415965867 931056007 328603885 862991829 82082068 344198824 831587464 105221046 931994543
3 10
3 6
2 5
1 8
2 5
2 4
1 4
2 9
4 7
2 10...

output:

1073741823 2
1073741823 2
1073741823 2
1073741823 0
1073741823 2
1073741823 0
536870911 2
1073741823 2
1073741823 0
536870911 2
1073741823 1
1073741823 2
1073741823 0
1073741823 0
1073741823 0
1073741823 2
1073741823 2
1073741823 0
1073741823 2
1073741823 0
1073741823 0
1073741823 0
1073741823 2
107...

result:

wrong answer 37120th numbers differ - expected: '1', found: '2'