QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#376100 | #6631. Maximum Bitwise OR | Mine_King | WA | 88ms | 151172kb | C++14 | 3.0kb | 2024-04-03 20:45:32 | 2024-04-03 20:45:33 |
Judging History
answer
// 君の人生は月明かりだ、有りがちだなんて言わせるものか
// Think twice, code once.
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eputchar(c) putc(c, stderr)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define eputs(str) fputs(str, stderr), putc('\n', stderr)
using namespace std;
int T, n, q, a[100005], op[100005][30], lst[100005][30], vis[100005];
struct SegmentTree {
#define ls(x) (x * 2)
#define rs(x) (x * 2 + 1)
int w[400005], l[400005], r[400005];
void build(int ll, int rr, int now = 1) {
w[now] = 0;
l[now] = ll, r[now] = rr;
if (ll == rr) return ;
int mid = (ll + rr) / 2;
build(ll, mid, ls(now)), build(mid + 1, rr, rs(now));
return ;
}
void update(int pos, int val, int now = 1) {
if (l[now] == r[now]) {w[now] = val; return ;}
int mid = (l[now] + r[now]) / 2;
if (pos <= mid) update(pos, val, ls(now));
else update(pos, val, rs(now));
w[now] = w[ls(now)] | w[rs(now)];
return ;
}
int query(int ll, int rr, int now = 1) {
if (ll <= l[now] && r[now] <= rr) return w[now];
int mid = (l[now] + r[now]) / 2, res = 0;
if (ll <= mid) res |= query(ll, rr, ls(now));
if (mid < rr) res |= query(ll, rr, rs(now));
return res;
}
#undef ls
#undef rs
} tr[30], all;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
cin >> n >> q;
all.build(1, n);
for (int i = 0; i < 30; i++) tr[i].build(1, n);
for (int i = 1; i <= n; i++) {
cin >> a[i];
all.update(i, a[i]);
memset(op[i], 0, sizeof(op[i]));
memcpy(lst[i], lst[i - 1], sizeof(lst[i]));
for (int j = 0; j < 30 && 1 << j <= a[i]; j++) {
tr[j].update(i, op[i][j] = a[i] ^ (a[i] - (1 << j)));
if (a[i] >> j & 1) lst[i][j] = i;
}
}
while (q--) {
int l, r;
cin >> l >> r;
int orsum = all.query(l, r), res = (1 << (__lg(orsum) + 1)) - 1, ans = 2;
if (orsum == res) {cout << res << " 0\n"; continue;}
for (int i = 0; i < 30; i++)
if (lst[lst[r][i] - 1][i] >= l && !vis[lst[r][i]]) {
vis[lst[r][i]] = 1;
int p = 0, q = 0;
if (lst[r][i] > l) p = all.query(l, lst[r][i] - 1);
if (lst[r][i] < r) q = all.query(lst[r][i] + 1, r);
for (int j = 0; j < 30; j++)
if ((p | op[lst[r][i]][j] | q) == res) {ans = 1; break;}
if (ans == 1) break;
}
for (int i = 0; i < 30; i++)
if (lst[lst[r][i] - 1][i] >= l) vis[lst[r][i]] = 0;
if (ans == 2) {
for (int i = 0; i < 30; i++)
if (lst[lst[r][i] - 1][i] >= l && !vis[lst[r][i]]) {
vis[lst[r][i]] = 1;
for (int j = 0; j < 30; j++)
tr[j].update(lst[r][i], 0);
}
for (int i = 0; i < 30; i++)
if ((tr[i].query(l, r) | orsum) == res) ans = 1;
for (int i = 0; i < 30; i++)
if (lst[lst[r][i] - 1][i] >= l && vis[lst[r][i]]) {
vis[lst[r][i]] = 0;
for (int j = 0; j < 30; j++)
tr[j].update(lst[r][i], op[lst[r][i]][j]);
}
}
cout << res << ' ' << ans << '\n';
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 149052kb
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: 55ms
memory: 151092kb
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: -100
Wrong Answer
time: 88ms
memory: 151172kb
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:
wrong answer 1556th numbers differ - expected: '2', found: '1'