QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#578642 | #6631. Maximum Bitwise OR | Kandarp08 | WA | 295ms | 3636kb | C++20 | 6.7kb | 2024-09-20 20:36:42 | 2024-09-20 20:36:42 |
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]);
}
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 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]);
}
void solve()
{
int n, q, i, j;
cin >> n >> q;
vector<int> a(n);
cin >> a;
vector<vector<int>> v(32, vector<int>(4 * n, 1000));
vector<vector<int>> prefix(32, vector<int>(n + 1, 0));
vector<vector<int>> special_bits(n);
vector<vector<int>> bits(32, vector<int>(n, -1));
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)
{
bits[j][i] = y;
break;
}
}
}
build(v[j], bits[j], 0, 0, 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);
}
while (q--)
{
int l, r;
cin >> l >> r;
--l, --r;
int highest = -1;
int lowest = -1;
int first_set = -1;
for (j = 31; j >= 0; --j)
{
if (prefix[j][r + 1] - prefix[j][l] > 0)
{
first_set = j;
break;
}
}
for (j = first_set - 1; j >= 0; --j)
{
if (prefix[j][r + 1] - prefix[j][l] == 0)
{
lowest = j;
if (highest == -1)
highest = j;
}
}
int ans = (1LL << (first_set + 1)) - 1;
if (lowest == -1)
{
cout << ans << " 0\n";
continue;
}
set<int> s1, s2;
for (j = 0; j <= 31; ++j)
{
if (prefix[j][r + 1] - prefix[j][l] == 1)
{
int next = lower_bound(prefix[j].begin() + l + 1, prefix[j].end(), prefix[j][l] + 1) - prefix[j].begin();
--next;
s1.insert(next);
special_bits[next].push_back(j);
}
}
for (int ind : s1)
{
if (special_bits[ind].size() > 1)
s2.insert(ind);
}
for (int ind : s2)
s1.erase(ind);
for (int ind : s1)
update(v[highest], 0, 0, n - 1, ind, 1000);
for (int ind : s2)
update(v[highest], 0, 0, n - 1, ind, 1000);
int mn_bit = getMin(v[highest], 0, 0, n - 1, l, r);
bool found = false;
if (mn_bit < lowest)
{
cout << ans << " 1\n";
found = true;
}
if (!found)
{
for (int ind : s1)
{
int num = a[ind];
bool flag = true;
for (j = lowest; j <= highest; ++j)
{
int mask = (1LL << j);
if (num & mask)
{
flag = false;
break;
}
}
if (flag)
{
for (j = highest + 1; j <= 31; ++j)
{
int mask = (1LL << j);
if ((num & mask) && special_bits[ind][0] == j)
{
found = true;
break;
}
else if (num & mask)
break;
}
if (found)
{
cout << ans << " 1\n";
break;
}
}
}
}
if (!found)
cout << ans << " 2\n";
for (int ind : s1)
{
update(v[highest], 0, 0, n - 1, ind, bits[highest][ind]);
special_bits[ind].clear();
}
for (int ind : s2)
{
update(v[highest], 0, 0, n - 1, ind, bits[highest][ind]);
special_bits[ind].clear();
}
}
}
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: 3540kb
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: 295ms
memory: 3540kb
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: 223ms
memory: 3612kb
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: 190ms
memory: 3636kb
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'