QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#260957 | #6631. Maximum Bitwise OR | ucup-team1198# | WA | 97ms | 3568kb | C++20 | 4.0kb | 2023-11-22 17:02:10 | 2023-11-22 17:02:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()
const int INF = (1 << 30);
const int K = 30;
struct SegTree {
vector<int> vals;
int N;
void reset(vector<int>& A) {
N = A.size();
vals.resize(4 * N);
build(0, 0, N, A);
}
void build(int v, int left, int right, vector<int>& A) {
if (left + 1 == right) {
vals[v] = A[left];
return;
}
int mid = (left + right) / 2;
build(2 * v + 1, left, mid, A);
build(2 * v + 2, mid, right, A);
vals[v] = min(vals[2 * v + 1], vals[2 * v + 2]);
}
int get(int v, int left, int right, int x, int y) {
if (y <= left || right <= x)
return INF;
if (x <= left && right <= y)
return vals[v];
int mid = (left + right) / 2;
return min(get(2 * v + 1, left, mid, x, y), get(2 * v + 2, mid, right, x, y));
}
};
SegTree trees[K + 1];
const int MAXN = 100100;
int pref_sums[K + 1][MAXN];
void solve() {
int n, q;
cin >> n >> q;
vector<int> A(n);
for (int j = 0; j < K; ++j)
pref_sums[j][0] = 0;
for (int i = 0; i < n; ++i) {
cin >> A[i];
for (int j = 0; j < K; ++j) {
int b = (A[i] >> j) & 1;
pref_sums[j][i + 1] = pref_sums[j][i] + b;
}
}
vector<int> B(n);
for (int j = 0; j <= K; ++j) {
for (int i = 0; i < n; ++i) {
B[i] = A[i] >= (1 << j) ? (A[i] & ((1 << j) - 1)) : INF;
}
trees[j].reset(B);
}
while (q--) {
int ll, rr;
cin >> ll >> rr;
--ll;
int have_or = 0;
int mx_bit = -1;
for (int j = 0; j < K; ++j) {
int cnt = pref_sums[j][rr] - pref_sums[j][ll];
if (cnt) {
have_or += 1 << j;
mx_bit = max(mx_bit, j);
}
}
if (have_or == (1 << (mx_bit + 1)) - 1) {
cout << have_or << ' ' << 0 << '\n';
continue;
}
// check if can in one op
vector<pair<int, int>> bad;
int mn_zero = mx_bit;
int mx_zero = -1;
for (int j = 0; j <= mx_bit; ++j) {
int cnt = pref_sums[j][rr] - pref_sums[j][ll];
if (cnt == 1) {
int id = lower_bound(pref_sums[j] + ll, pref_sums[j] + rr, pref_sums[j][ll] + 1) - pref_sums[j];
bad.emplace_back(id - 1, j);
}
if (cnt == 0) {
mn_zero = min(mn_zero, j);
mx_zero = max(mx_zero, j);
}
}
++mx_zero;
bool pos = false;
sort(bad.begin(), bad.end());
for (int i = 0; i < bad.size(); ++i) {
if ((i == 0 || bad[i - 1].first != bad[i].first) && (i + 1 == bad.size() || bad[i + 1].first != bad[i].first)) {
// unique bad
if (bad[i].second >= mx_zero && (A[i] & ((1 << bad[i].second) - 1)) < (1 << mn_zero)) {
//cerr << bad[i].first << ' ' << bad[i].second << "here\n";
pos = true;
}
}
}
int pr = ll;
for (int i = 0; i < bad.size(); ++i) {
int cur = bad[i].first;
if (pr < cur) {
// check on subsegment
if (trees[mx_zero].get(0, 0, n, pr, cur) < (1 << mn_zero)) {
//cerr << pr << ' ' << cur << " here segm\n";
pos = true;
}
}
pr = cur + 1;
}
if (pr < rr) {
if (trees[mx_zero].get(0, 0, n, pr, rr) < (1 << mn_zero)) {
//cerr << pr << ' ' << rr << " here segm\n";
pos = true;
}
}
cout << ((1 << (mx_bit + 1)) - 1) << ' ' << (pos ? 1 : 2) << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3528kb
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: 97ms
memory: 3568kb
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: 96ms
memory: 3564kb
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 81216th numbers differ - expected: '2', found: '1'