QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#284617 | #6631. Maximum Bitwise OR | Misuki | WA | 68ms | 3460kb | C++20 | 4.1kb | 2023-12-16 14:06:44 | 2023-12-16 14:06:44 |
Judging History
answer
#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
//#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)
namespace R = std::ranges;
namespace V = std::views;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = tuple<int, int, int>;
using ldb = long double;
//#define double ldb
template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
for(const T &X : arr)
os << X << ' ';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
for(const T &X : vec)
os << X << ' ';
return os;
}
/**
* template name: sparseTable
* author: Misuki
* last update: 2023/11/30
* verify: LibraryChecker - Static RMQ
*/
template<class T>
struct sparseTable{
vector<vector<T> > table;
function<T(T, T)> comb;
int size = 0;
sparseTable(vector<T> &base, function<T(T, T)> _comb) {
comb = _comb;
size = base.size();
table.resize(bit_width((unsigned)size), std::vector<T>(size));
table[0] = base;
for(int i = 1; i < ssize(table); i++) {
for(int j = 0; j < size; j++) {
if (j + (1 << (i - 1)) < size)
table[i][j] = comb(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
else
table[i][j] = table[i - 1][j];
}
}
}
//query range in [l, r)
T query(int l, int r) {
int range = bit_width((unsigned)(r - l)) - 1;
return comb(table[range][l], table[range][r - (1 << range)]);
}
};
struct M {
int orSum, hb;
array<int, 30> rb;
M(unsigned x = 0) : orSum(x), hb(bit_width(x) - 1) {
fill(rb.begin(), rb.end(), -1);
for(int bit = 0, p = 0; bit < 30; bit++) {
if (x >> bit & 1) {
for(int i = p; i <= bit; i++)
rb[i] = bit;
p = bit + 1;
}
}
}
};
signed main() {
ios::sync_with_stdio(false), cin.tie(NULL);
int t; cin >> t;
while(t--) {
int n, q; cin >> n >> q;
vector<int> a(n);
for(int &x : a)
cin >> x;
vector<M> init(n);
for(int i = 0; i < n; i++)
init[i] = M(a[i]);
sparseTable<M> st(init, [](M l, M r) {
M res;
res.orSum = l.orSum | r.orSum, res.hb = max(l.hb, r.hb);
for(int i = 0; i < 30; i++)
res.rb[i] = max(l.rb[i], r.rb[i]);
return res;
});
while(q--) {
int l, r; cin >> l >> r;
M res = st.query(l - 1, r);
int ans = (1 << (res.hb + 1)) - 1, ope;
if (res.orSum == ans) {
ope = 0;
} else {
int d = 30, u = -1;
for(int bit = 0; bit < res.hb; bit++)
if (~res.orSum >> bit & 1)
d = min(d, bit), u = bit;
if (res.rb[d] >= u)
ope = 1;
else
ope = 2;
}
cout << ans << ' ' << ope << '\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3448kb
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: 68ms
memory: 3460kb
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: 63ms
memory: 3444kb
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'