QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#585439 | #6631. Maximum Bitwise OR | Kandarp08 | WA | 299ms | 3668kb | C++20 | 6.1kb | 2024-09-23 20:50:21 | 2024-09-23 20:50:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define MOD 1000000007
#define HASH_MOD1 998244353
#define HASH_MOD2 1000000009
#define HASH 31
#define ll long long
#define int long long
typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
template <typename T>
istream& operator>> (istream& in, vector<T>& a)
{
for (int i = 0; i < a.size(); ++i)
in >> a[i];
return in;
}
template <typename T>
ostream& operator<< (ostream& out, vector<T>& a)
{
for (int i = 0; i < a.size(); ++i)
out << a[i] << " ";
out << "\n";
return out;
}
template <typename T>
ostream& operator<< (ostream& out, set<T>& s)
{
for (int x : s)
out << x << " ";
out << "\n";
return out;
}
template <typename T>
ostream& operator<< (ostream& out, vector<pair<T, T>>& v)
{
for (int i = 0; i < v.size(); ++i)
out << v[i].first << " " << v[i].second << "\n";
return out;
}
ll modinv(ll a)
{
return a <= 1 ? a : MOD - (MOD / a) * modinv(MOD % a) % MOD;
}
ll binpow(ll x, ll y)
{
ll ans = 1;
while (y > 0)
{
if (y % 2 == 1)
ans *= x;
x = x * x;
y = y / 2;
x %= MOD;
ans %= MOD;
}
return ans;
}
void build(vector<int>& st, vector<int>& a, int i, int ss, int se)
{
if (ss == se)
{
st[i] = a[ss];
return;
}
int mid = (ss + se) / 2;
build(st, a, 2 * i + 1, ss, mid);
build(st, a, 2 * i + 2, mid + 1, se);
st[i] = min(st[2 * i + 1], st[2 * i + 2]);
}
void update(vector<int>& st, int i, int ss, int se, int pos, int x)
{
if (pos < ss || pos > se)
return;
if (ss == se)
{
st[i] = x;
return;
}
int mid = (ss + se) / 2;
update(st, 2 * i + 1, ss, mid, pos, x);
update(st, 2 * i + 2, mid + 1, se, pos, x);
st[i] = min(st[2 * i + 1], st[2 * i + 2]);
}
int getMin(vector<int>& st, int i, int ss, int se, int l, int r)
{
if (l > se || r < ss)
return 1000;
if (l <= ss && se <= r)
return st[i];
int mid = (ss + se) / 2;
return min(getMin(st, 2 * i + 1, ss, mid, l, r), getMin(st, 2 * i + 2, mid + 1, se, l, r));
}
void solve()
{
int n, q, i, j;
cin >> n >> q;
vector<int> a(n);
cin >> a;
vector<vector<int>> prefix(32, vector<int>(n + 1, 0));
vector<vector<int>> st(32, vector<int>(4 * n));
vector<vector<int>> next_highest(32, vector<int>(n, -1));
for (j = 0; j < 32; ++j)
{
int mask = (1LL << j);
for (i = 1; i <= n; ++i)
prefix[j][i] = prefix[j][i - 1] + ((a[i - 1] & mask) > 0);
}
for (j = 0; j < 32; ++j)
{
for (i = 0; i < n; ++i)
{
for (int y = j; y >= 0; --y)
{
int mask = (1LL << y);
if (a[i] & mask)
{
next_highest[j][i] = y;
break;
}
}
}
build(st[j], next_highest[j], 0, 0, n - 1);
}
while (q--)
{
int l, r;
cin >> l >> r;
--l, --r;
int lowest = -1;
int highest = -1;
int first_set = -1;
for (j = 31; j >= 0; --j)
{
if (prefix[j][r + 1] - prefix[j][l] > 0)
{
first_set = j;
break;
}
}
int ans = (1LL << (first_set + 1)) - 1;
for (j = first_set - 1; j >= 0; --j)
{
if (prefix[j][r + 1] - prefix[j][l] == 0)
{
lowest = j;
if (highest == -1)
highest = j;
}
}
if (lowest == -1)
{
cout << ans << " 0\n";
continue;
}
set<int> s;
map<int, vector<int>> special_bits;
for (j = 0; j < 32; ++j)
{
if (prefix[j][r + 1] - prefix[j][l] == 1)
{
int res = lower_bound(prefix[j].begin(), prefix[j].end(), prefix[j][l] + 1) - prefix[j].begin() - 1;
s.insert(res);
special_bits[res].push_back(j);
update(st[highest], 0, 0, n - 1, res, 1000);
}
}
int mn_bit = getMin(st[highest], 0, 0, n - 1, l, r);
for (int ind : s)
update(st[highest], 0, 0, n - 1, ind, next_highest[highest][ind]);
if (mn_bit < lowest)
{
cout << ans << " 1\n";
continue;
}
bool found = false;
for (int ind : s)
{
if (special_bits[ind].size() == 1)
{
for (j = lowest; j < 32; ++j)
{
int mask = (1LL << j);
if (a[ind] & mask)
{
if (j > highest && special_bits[ind][0] == j)
{
found = true;
for (j = lowest; j <= highest; ++j)
{
mask = (1LL << j);
if (a[ind] & mask)
{
found = false;
break;
}
}
}
break;
}
}
}
if (found)
{
cout << ans << " 1\n";
break;
}
}
if (!found)
cout << ans << " 2\n";
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
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: 299ms
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: 0
Accepted
time: 230ms
memory: 3668kb
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:
ok 200000 numbers
Test #4:
score: -100
Wrong Answer
time: 210ms
memory: 3524kb
input:
33333 3 3 925088809 339284112 289540728 3 3 1 3 1 1 3 3 422399522 892365243 216341776 3 3 3 3 1 2 3 3 668932010 837523227 840095874 1 3 1 3 3 3 3 3 731584574 357877180 359063739 1 1 1 1 3 3 3 3 463358343 833924976 847087403 2 3 3 3 1 2 3 3 377154649 772000701 656357011 2 3 1 2 2 3 3 3 977492169 5540...
output:
536870911 2 1073741823 2 1073741823 2 268435455 2 268435455 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 536870911 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 1073741823 2 67108863 2 1073741823 2 1073741...
result:
wrong answer 20788th numbers differ - expected: '2', found: '1'