QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135261 | #6631. Maximum Bitwise OR | salvator_noster# | WA | 142ms | 103356kb | C++14 | 4.0kb | 2023-08-05 13:17:28 | 2023-08-05 13:17:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 5;
const int MAX_M = 32;
int N, M, Q, a[MAX_N], sor[1 << 18];
struct SegmentNode{
int cnt, minv, idx;
SegmentNode (int a = 0, int b = 0, int c = 0) : cnt(a), minv(b), idx(c) {}
void init(int x, int k, int l) {
cnt = x >> k & 1;
if (x < (1 << k)) minv = 32;
else {
if (k) {
int tmp = x & (1 << k) - 1;
if (!tmp) minv = 0;
else minv = 32 - __builtin_clz(tmp);
}else {
minv = 0;
}
}
idx = cnt ? l : 0;
}
inline SegmentNode operator + (const SegmentNode &x) const {
return (SegmentNode){cnt + x.cnt, min(minv, x.minv), idx ^ x.idx};
}
}node[MAX_M][1 << 18], b[32];
void segment_build1(int i, int l, int r) {
if (l == r) {
sor[i] = a[l];
return ;
}
int mid = l + r >> 1;
segment_build1(i << 1, l, mid);
segment_build1(i << 1 | 1, mid + 1, r);
sor[i] = sor[i << 1] | sor[i << 1 | 1];
}
int segment_query1(int i, int l, int r, int ql, int qr) {
if (l < ql || r > qr) {
int res = 0, mid = l + r >> 1;
if (ql <= mid) res = segment_query1(i << 1, l, mid, ql, qr);
if (mid < qr) res |= segment_query1(i << 1 | 1, mid + 1, r, ql, qr);
return res;
}else {
return sor[i];
}
}
void segment_build2(int k, int i, int l, int r) {
if (l == r) {
node[k][i].init(a[l], k, l);
return ;
}
int mid = l + r >> 1;
segment_build2(k, i << 1, l, mid);
segment_build2(k, i << 1 | 1, mid + 1, r);
node[k][i] = node[k][i << 1] + node[k][i << 1 | 1];
}
void segment_modify2(int k, int i, int l, int r, int x, int v) {
if (l == r) {
node[k][i].init(v, k, l);
return ;
}
int mid = l + r >> 1;
mid < x ? segment_modify2(k, i << 1 | 1, mid + 1, r, x, v) : segment_modify2(k, i << 1, l, mid, x, v);
node[k][i] = node[k][i << 1] + node[k][i << 1 | 1];
}
SegmentNode segment_query2(int k, int i, int l, int r, int ql, int qr) {
if (l < ql || r > qr) {
int mid = l + r >> 1;
SegmentNode res(0, 32, 0);
if (ql <= mid) res = segment_query2(k, i << 1, l, mid, ql, qr);
if (mid < qr) res = res + segment_query2(k, i << 1 | 1, mid + 1, r, ql, qr);
return res;
}else {
return node[k][i];
}
}
int main() {
int T;
scanf("%d", &T);
while (T --) {
scanf("%d%d", &N, &Q);
M = 0;
for (int i = 1; i <= N; i ++) {
scanf("%d", a + i);
if (a[i]) M = max(M, 31 - __builtin_clz(a[i]));
}
segment_build1(1, 1, N);
for (int i = 0; i <= M; i ++) {
segment_build2(i, 1, 1, N);
}
while (Q --) {
int l, r;
scanf("%d%d", &l, &r);
int res = segment_query1(1, 1, N, l, r);
if (!res) {
printf("0 0\n");
continue;
}
int t = 31 - __builtin_clz(res);
if (res == (1 << t + 1) - 1) {
printf("%d 0\n", res);
continue;
}
for (int i = 0; i <= t; i ++) {
b[i] = segment_query2(i, 1, 1, N, l, r);
}
int x, y;
for (x = t; res >> x & 1; x --);
for (y = 0; res >> y & 1; y ++);
for (int i = 0; i < t; i ++) {
if (b[i].cnt == 1 && a[b[i].idx] >= (1 << i + 1)) segment_modify2(x, 1, 1, N, b[i].idx, 0);
}
int cur = segment_query2(x, 1, 1, N, l, r).minv;
if (cur <= y) printf("%d 1\n", (1 << t + 1) - 1);
else printf("%d 2\n", (1 << t + 1) - 1);
for (int i = 0; i < t; i ++)
if (b[i].cnt == 1 && a[b[i].idx] >= (1 << i + 1)) segment_modify2(x, 1, 1, N, b[i].idx, a[b[i].idx]);
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 101916kb
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: 101ms
memory: 102852kb
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: 128ms
memory: 102056kb
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: 142ms
memory: 103356kb
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 4510th numbers differ - expected: '2', found: '1'