QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135076 | #6631. Maximum Bitwise OR | salvator_noster# | WA | 96ms | 69848kb | C++14 | 3.4kb | 2023-08-05 11:12:08 | 2023-08-05 11:12:11 |
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], b[MAX_M], f[MAX_M], sor[1 << 18];
struct SegmentNode{
int cnt, minv;
SegmentNode (int a = 0, int b = 0) : cnt(a), minv(b) {}
void init(int x, int k) {
cnt = x >> k & 1;
if (!cnt) minv = k;
else {
if (k) {
int tmp = x & (1 << k) - 1;
if (!tmp) minv = 0;
else minv = 32 - __builtin_clz(tmp);
}else {
minv = 0;
}
}
}
inline SegmentNode operator + (const SegmentNode &x) const {
return (SegmentNode){cnt + x.cnt, min(minv, x.minv)};
}
}node[MAX_M][1 << 18];
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);
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];
}
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);
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);
for (int i = 0; i <= M; i ++) {
f[i] = 0x3f3f3f3f;
SegmentNode tmp = segment_query2(i, 1, 1, N, l, r);
if (tmp.cnt > 1) b[i] = tmp.minv;
else b[i] = i;
}
f[M] = 0;
for (int i = M - 1; i >= 0; i --) {
if (res >> i & 1) {
f[i] = f[i + 1];
continue;
}
for (int j = i + 1; j <= M; j ++) {
if (b[j] <= i) {
f[i] = min(f[i], f[j] + 1);
}
}
if (f[i] < 0x3f3f3f3f) res |= 1 << i;
else f[i] = f[i + 1];
}
printf("%d %d\n", res, f[0]);
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 5ms
memory: 69848kb
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: -100
Wrong Answer
time: 96ms
memory: 69388kb
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:
924704060 0 149840457 0 515267304 0 635378394 0 416239424 0 960156404 0 431278082 0 629009153 0 140374311 0 245014761 0 445512399 0 43894730 0 129731646 0 711065534 0 322643984 0 482420443 0 202661591 0 529979773 0 520572847 0 500838890 0 224446016 0 228171383 0 973333717 0 8493236 0 622547486 0 677...
result:
wrong answer 1st numbers differ - expected: '1073741823', found: '924704060'