QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#285043 | #6631. Maximum Bitwise OR | Misuki | WA | 75ms | 3812kb | C++20 | 5.8kb | 2023-12-16 16:19:11 | 2023-12-16 16:19:12 |
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) {
if (l == r) return 0;
int range = bit_width((unsigned)(r - l)) - 1;
return comb(table[range][l], table[range][r - (1 << range)]);
}
};
struct BIT {
int C;
vector<int> vec;
BIT(int _C) : C(_C), vec(_C) {};
void add(int i, int d) {
for(i++; i < C; i += i & (-i))
vec[i] += d;
}
int query(int i) {
int res = 0;
for(i++; i; i -= i & (-i))
res += vec[i];
return res;
}
};
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<vector<array<int, 2>>> qry(n + 1);
for(int i = 0; i < q; i++) {
int l, r; cin >> l >> r;
qry[l - 1].push_back({r - 1, i});
}
sparseTable<int> st(a, [](int l, int r) { return l | r; });
vector<vector<array<int, 2>>> ope(n + 1);
{
for(int i = 0; i < n; i++) {
vector<int> lb = {i + 1, i};
while(lb.back() != 0) {
int l = 0, r = lb.back() - 1;
while(l < r) {
int mid = (l + r) / 2;
if (st.query(mid, i) == st.query(lb.back() - 1, i))
r = mid;
else
l = mid + 1;
}
lb.emplace_back(l);
}
R::reverse(lb);
vector<int> rb = {i, i + 1};
while(rb.back() != n) {
int l = rb.back() + 1, r = n;
while(l < r) {
int mid = (l + r) / 2;
if (st.query(i + 1, mid + 1) != st.query(i + 1, rb.back() + 1))
r = mid;
else
l = mid + 1;
}
rb.emplace_back(l);
}
for(int j = 0; j + 1 < ssize(lb); j++) {
for(int k = 0; k + 1 < ssize(rb); k++) {
int orSum = st.query(lb[j], i) | st.query(i + 1, rb[k] + 1);
bool ok = false;
for(int bit = 0, p = 0; bit < 30; bit++) {
if (a[i] >> bit & 1) {
unsigned tmp = orSum | ((1 << (bit + 1)) - (1 << p));
//cout << "check " << bitset<10>(tmp) << '\n';
ok = ok or bit_width(tmp) == popcount(tmp);
p = bit + 1;
}
}
if (ok) {
//cout << "add rect " << lb[j] << ' ' << lb[j + 1] << ", " << rb[k] << ' ' << rb[k + 1] << '\n';
ope[lb[j]].push_back({rb[k], 1});
ope[lb[j]].push_back({rb[k + 1], -1});
ope[lb[j + 1]].push_back({rb[k], -1});
ope[lb[j + 1]].push_back({rb[k + 1], 1});
}
}
}
}
}
BIT bit(n + 2);
vector<array<int, 2>> ans(q);
for(int l = 0; l <= n; l++) {
for(auto [i, d] : ope[l])
bit.add(i, d);
for(auto [r, i] : qry[l]) {
if (int target = (1 << bit_width((unsigned)st.query(l, r + 1))) - 1; target == st.query(l, r + 1))
ans[i] = {target, 0};
else if (bit.query(r) >= 1)
ans[i] = {target, 1};
else
ans[i] = {target, 2};
}
}
for(auto [x, y] : ans)
cout << x << ' ' << y << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
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: -100
Wrong Answer
time: 75ms
memory: 3812kb
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 1 268435455 1 536870911 1 1073741823 1 536870911 1 1073741823 1 536870911 1 1073741823 1 268435455 1 268435455 1 536870911 1 67108863 1 134217727 1 1073741823 1 536870911 1 536870911 1 268435455 1 536870911 1 536870911 1 536870911 1 268435455 1 268435455 1 1073741823 1 16777215 1 10737418...
result:
wrong answer 2nd numbers differ - expected: '2', found: '1'