QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#118978 | #6631. Maximum Bitwise OR | 8BQube# | ML | 0ms | 0kb | C++20 | 3.0kb | 2023-07-04 17:10:59 | 2023-07-04 17:10:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
const int L = 30;
int arr[100005], cnt[100005][L];
typedef array<vector<int>, L> T;
vector<T> mi(400005);
T operator+(const T &a, const T &b) {
T res;
for (int i = 0; i < L; ++i) {
for (int j = 0, k = 0; j < SZ(a[i]) || k < SZ(b[i]);)
if (k >= SZ(b[i]) || (j < SZ(a[i]) && (a[i][j] & ((1 << (i + 1)) - 1)) < (b[i][k] & ((1 << (i + 1)) - 1))))
res[i].pb(a[i][j++]);
else
res[i].pb(b[i][k++]);
if (SZ(res[i]) > L + 5)
res[i].resize(L + 5);
}
return res;
}
void build(int l, int r, int rt) {
if (l == r) {
for (int i = 0; i < L; ++i)
if (arr[l] > (1 << i) && (arr[l] >> i & 1) == 0) {
mi[rt][i] = vector<int>(1, arr[l]);
}
else
mi[rt][i].clear();
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
mi[rt] = mi[rt << 1] + mi[rt << 1 | 1];
}
T query(int L, int R, int l, int r, int rt) {
if (L <= l && R >= r) return mi[rt];
int mid = (l + r) >> 1;
if (R <= mid) return query(L, R, l, mid, rt << 1);
if (L > mid) return query(L, R, mid + 1, r, rt << 1 | 1);
return query(L, R, l, mid, rt << 1) + query(L, R, mid + 1, r, rt << 1 | 1);
}
void solve() {
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; ++i)
cin >> arr[i];
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < L; ++j) {
cnt[i][j] = cnt[i - 1][j];
if (arr[i] >> j & 1) ++cnt[i][j];
}
}
build(1, n, 1);
while (q--) {
int l, r;
cin >> l >> r;
int fin = 0, ans = 0, d = -1, u = -1;
vector<int> curcnt(L);
for (int i = 0; i < L; ++i)
curcnt[i] = cnt[r][i] - cnt[l - 1][i];
for (int i = L - 1; i >= 0; --i)
if (curcnt[i] > 0) {
fin = (1 << (i + 1)) - 1;
break;
}
for (int i = __lg(fin); i >= 0; --i) {
if (curcnt[i] == 0) {
if (!~u) u = i;
d = i;
}
}
if (~u) {
ans = 2;
T res = query(l, r, 1, n, 1);
for (int i : res[u]) {
for (int j = 0; j < L; ++j)
curcnt[j] -= i >> j & 1;
int tmp = i ^ (i - (1 << d));
for (int j = 0; j < L; ++j)
if (curcnt[j])
tmp |= 1 << j;
if (tmp == fin) ans = 1;
for (int j = 0; j < L; ++j)
curcnt[j] += i >> j & 1;
}
}
cout << fin << " " << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
详细
Test #1:
score: 0
Memory Limit Exceeded
input:
1 3 2 10 10 5 1 2 1 3