QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#286751 | #6631. Maximum Bitwise OR | jrjyy | RE | 0ms | 0kb | C++20 | 3.4kb | 2023-12-18 15:55:02 | 2023-12-18 15:55:03 |
answer
#include <bits/stdc++.h>
using i64 = long long;
template <typename T, typename Cmp = std::less<T>>
struct RMQ {
const Cmp cmp{};
int n;
std::vector<std::vector<T>> a;
RMQ() = default;
RMQ(const std::vector<T> &v) {
init(v);
}
RMQ(const std::vector<T> &v, const Cmp &cmp_) : cmp{cmp_} {
init(v);
}
void init(const std::vector<T> &ini) {
n = int(ini.size());
int lg = std::__lg(n);
a.assign(lg + 1, std::vector<T>(n));
a[0] = ini;
for (int i = 0; i < lg; ++i) {
for (int j = 0; j <= n - (2 << i); ++j) {
a[i + 1][j] = std::min(a[i][j], a[i][j + (1 << i)], cmp);
}
}
}
T operator()(int l, int r) const {
int k = std::__lg(r - l);
return std::min(a[k][l], a[k][r - (1 << k)], cmp);
}
};
constexpr int K = 30;
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];
}
std::vector<std::array<int, K>> pre(n + 1), suf(n + 1);
pre[0].fill(-1);
for (int i = 0; i < n; ++i) {
pre[i + 1] = pre[i];
for (int x = 0; x < 30; ++x) {
if (a[i] >> x & 1) {
pre[i + 1][x] = i;
}
}
}
suf[n].fill(n);
for (int i = n - 1; i >= 0; --i) {
suf[i] = suf[i + 1];
for (int x = 0; x < 30; ++x) {
if (a[i] >> x & 1) {
suf[i][x] = i;
}
}
}
std::vector<int> v(n, -1);
std::array<RMQ<int, std::greater<>>, K> rmq;
for (int x = K - 1; x >= 0; --x) {
for (int i = 0; i < n; ++i) {
if (a[i] >> x & 1) {
v[i] = x;
}
}
rmq[x].init(v);
}
while (q--) {
int l, r;
std::cin >> l >> r;
--l;
int tot = 0, ans = 0;
for (int x = 0; x < K; ++x) {
if (pre[r][x] >= l) {
tot |= 1 << x;
ans = (2 << x) - 1;
}
}
int step = 2;
if (tot == ans) {
step = 0;
} else {
std::vector<std::tuple<int, int, int>> pos{{r, -1, -1}};
int vp = 0, vs = tot;
for (int x = 0; x < K; ++x) {
if (suf[l][x] < r) {
pos.emplace_back(suf[l][x], x, 1);
}
if (pre[r][x] >= l) {
pos.emplace_back(pre[r][x] - 1, x, -1);
}
}
std::sort(pos.begin(), pos.end());
int lst = l;
for (auto [nxt, v, o] : pos) {
int cur = (vp | vs) ^ ans;
int lo = __builtin_ctz(cur), hi = std::__lg(cur);
if (rmq[lo](lst, nxt) >= hi) {
step = 1;
break;
}
lst = nxt;
if (o == 1) {
vp ^= 1 << v;
} else if (v != -1) {
vs ^= 1 << v;
}
}
}
std::cout << ans << " " << step << "\n";
}
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
1 3 2 10 10 5 1 2 1 3