QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181313 | #6631. Maximum Bitwise OR | UrgantTeam# | WA | 220ms | 30112kb | C++23 | 3.8kb | 2023-09-16 17:40:53 | 2023-09-16 17:40:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int const maxn = 1e5 + 5, maxb = 30;
int a[maxn];
int nxt[maxn][maxb];
int cur[maxb][maxn];
int jump[maxb][maxn];
int get(int j, int l, int r) {
int pos = l;
if (l > r) return -1;
while (jump[j][pos] <= r) pos = jump[j][pos];
return cur[j][pos];
}
main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#endif // HOME
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q, l, r, t;
cin >> t;
while (t--) {
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int j = 0; j < maxb; j++) nxt[n + 1][j] = n + 1;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < maxb; j++) {
if ((a[i]>>j)&1) nxt[i][j] = i;
else nxt[i][j] = nxt[i + 1][j];
}
}
for (int i = 1; i <= n; i++) {
int b = 0;
for (int j = 0; j < maxb; j++) cur[j][i] = -1;
while (b < maxb) {
if ((a[i]>>b)&1) {
b++;
continue;
}
int now = b;
while (now < maxb && ((a[i]>>now)&1) == 0) {
now++;
}
if (now != maxb) {
for (int j = b; j < now; j++) cur[j][i] = now - 1;
}
b = now;
}
}
for (int j = 0; j < maxb; j++) {
for (int i = n; i >= 1; i--) {
jump[j][i] = i + 1;
while (jump[j][i] <= n && cur[j][jump[j][i]] <= cur[j][i]) jump[j][i] = jump[j][jump[j][i]];
}
}
for (int i = 1; i <= q; i++) {
cin >> l >> r;
int value = 0;
for (int j = 0; j < maxb; j++) {
if (nxt[l][j] <= r) value |= (1 << j);
}
int x = 1;
while (x <= value) x *= 2;
if (x - 1 == value) {
cout << value << " " << 0 << '\n';
continue;
}
int imin = maxb, imax = 0;
for (int j = 0; j < maxb && (1 << j) <= value; j++) {
if ((value>>j)&1) continue;
imin = min(imin, j), imax = j;
}
vector < int > position = {l - 1, r + 1};
for (int j = 0; j < maxb && (1 << j) <= value; j++) {
if ((value>>j)&1) {
if (nxt[nxt[l][j] + 1][j] > r) position.push_back(nxt[l][j]);
}
}
sort(position.begin(), position.end());
int best = -1;
for (int j = 1; j < position.size(); j++) {
best = max(best, get(imin, position[j - 1] + 1, position[j] - 1));
if (j < position.size() - 1) {
int flag = 1;
if (a[position[j]] < (1 << imin)) continue;
int nxt_value = (a[position[j]]^(a[position[j]] - (1 << imin)));
for (int bb = 0; bb < maxb; bb++) {
if (((value>>bb)&1) == 0) continue;
if (imin <= bb && bb <= imax) {
if (((nxt_value>>bb)&1) == 0) flag = 0;
}
if (nxt[nxt[l][bb] + 1][bb] > r) {
if (((a[position[j]]>>bb)&1) && ((nxt_value>>bb)&1) == 0) {
flag = 0;
}
}
}
if (flag) best = maxb;
}
}
if (best >= imax) cout << x - 1 << " " << 1 << '\n';
else cout << x - 1 << " " << 2 << '\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 26016kb
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: 193ms
memory: 30112kb
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: 218ms
memory: 26008kb
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: 220ms
memory: 26096kb
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 18810th numbers differ - expected: '2', found: '1'