QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#130394 | #6631. Maximum Bitwise OR | RobeZH# | WA | 59ms | 5684kb | C++14 | 3.1kb | 2023-07-23 23:19:55 | 2023-07-23 23:19:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define subnb true
#define Lnb true
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define lson(x) 2*x+1
#define rson(x) 2*x+2
const int N = (int)1e5 + 50, M = 32;
const int INF = (int)1e9;
struct node {
int mx[M], sum;
void init(int x) {
// cout << "initing " << x << ", " << bitset<20>(x) << endl;
sum = x;
fill(mx, mx + M, -INF);
int r = M - 1;
while(r >= 0 && !(x >> r & 1)) r--;
for (int i = r; i >= 0; i = r) {
r = i - 1;
while(r >= 0 && !(x >> r & 1)) r--;
// mx[i] = r + 1;
// cout << i << " to " << r + 1 << endl;
mx[r + 1] = i;
}
// rep(i, 0, M) {
// cout << i << ": " << mx[i] << "\n";
// }
}
};
node empty_node() {
node res;
rep(i, 0, M) res.mx[i] = -INF;
res.sum = 0;
return res;
}
node add(node L, node R) {
node res;
rep(i, 0, M) {
res.mx[i] = max(L.mx[i], R.mx[i]);
}
res.sum = L.sum | R.sum;
return res;
}
int val[N];
struct Tree {
node dat[N * 4];
void init(int x, int l, int r) {
if(l == r) {
dat[x].init(val[l]);
return ;
}
int mid = (l + r) / 2;
init(lson(x), l, mid);
init(rson(x), mid + 1, r);
dat[x] = add(dat[lson(x)], dat[rson(x)]);
}
node query(int a, int b, int x, int l, int r) {
if(r < a || b < l) return empty_node();
int mid = (l + r) / 2;
if(a <= l && r <= b) return dat[x];
// node res;
return add(query(a, b, lson(x), l, mid), query(a, b, rson(x), mid + 1, r));
}
} tree;
int n, q;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
while(T--) {
cin >> n >> q;
rep(i, 0, n) cin >> val[i];
tree.init(0, 0, n - 1);
rep(i, 0, q) {
int l, r; cin >> l >> r; l--, r--;
node nd = tree.query(l, r, 0, 0, n - 1);
int sum = nd.sum;
int high = -1;
while((1 << (high + 1) <= sum)) high++;
int want = (1 << (high + 1)) - 1;
if(sum == want) {
cout << want << " " << 0 << '\n';
} else {
int lb = INF, rb = -1;
rep(j, 0, high + 1) {
if(!(sum >> j & 1)) {
lb = min(lb, j);
rb = max(rb, j);
}
}
bool found = false;
rep(j, 0, lb + 1) {
found |= nd.mx[j] >= rb;
}
cout << want << " ";
if(found) cout << 1 << "\n";
else cout << 2 << '\n';
}
}
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5668kb
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: 59ms
memory: 5684kb
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: 46ms
memory: 3660kb
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'