QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#122755 | #6631. Maximum Bitwise OR | wnmrmr# | WA | 875ms | 3600kb | C++23 | 4.5kb | 2023-07-11 01:49:46 | 2023-07-11 01:49:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) //cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#define pb push_back
#define int long long
int n, q;
void solve () {
cin >> n >> q;
vector<int> a (n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector last (n + 1, vector<int> (30));
vector sum_bit (n + 1, vector<int> (30));
vector freq (n + 1, vector (30, vector<int> (30)));
vector acum (n + 1, vector (30, vector<int> (30)));
for (int i = 1; i <= n; i++) {
last[i] = last[i - 1];
sum_bit[i] = sum_bit[i - 1];
acum[i] = acum[i - 1];
// vamos adicionar o numero
for (int l = 0; l < 30; l++) {
// atualizamos o ultimo
if ((1 << l) & a[i]) last[i][l] = i, sum_bit[i][l]++;
// atualizamos freq
freq[i][l][l]++;
for (int r = l + 1; r < 30; r++) {
if ((1 << r) & a[i]) break;
else freq[i][l][r]++;
}
}
for (int l = 0; l < 30; l++)
for (int r = l; r < 30; r++)
acum[i][l][r] += freq[i][l][r];
}
auto get_f = [&] (int l, int r) {
vector f (30, vector<int> (30));
for (int i = 0; i < 30; i++)
for (int j = i; j < 30; j++)
f[i][j] = acum[r][i][j] - acum[l - 1][i][j];
return f;
};
auto get_sum = [&] (int l, int r) {
vector<int> sum (30);
for (int i = 0; i < 30; i++) sum[i] = sum_bit[r][i] - sum_bit[l - 1][i];
return sum;
};
while (q--) {
int l, r; cin >> l >> r;
auto f = get_f (l, r);
auto sum = get_sum (l, r);
int mx = 29;
while (mx >= 0 && !sum[mx]) mx--;
// dbg (mx);
cout << (1 << (mx + 1)) - 1 << " ";
if (mx < 0) {
// se o or eh 0
cout << "0\n";
continue;
}
bool ok = true;
for (int j = 0; j <= mx; j++) if (!sum[j]) ok = false;
if (ok) {
cout << "0\n";
continue;
}
// senao, temos que ver se a resposta eh 1
// vamos fazer operacao com cada cara 1, e depois vamos ver
vector<int> esp;
for (int j = 0; j < 30; j++) if (sum[j] == 1) {
// se esse cara eh unico
// dbg (last[r][j]);
esp.pb (last[r][j]);
}
// dbg (esp.size ());
if (esp.size ()) {
sort (all (esp));
esp.resize (unique (all (esp)) - esp.begin ());
}
ok = false;
for (auto id : esp) {
// retiramos e vermos se eh possivel
for (int i = 0; i < 30; i++) {
if ((1 << i) & a[id]) sum[i]--;
for (int j = i; j < 30; j++) {
f[i][j] -= freq[id][i][j];
}
}
// temos que ver se ainda existe um cara que preenche
int esq = 30, dir = -1;
for (int j = 0; j <= mx; j++) if (!sum[j]) {
esq = min (esq, j);
dir = max (dir, j);
}
// dbg (esq, dir);
if (freq[id][esq][dir]) {
ok = true;
}
for (int i = 0; i < 30; i++) {
if ((1 << i) & a[id]) sum[i]++;
for (int j = i; j < 30; j++) {
f[i][j] += freq[id][i][j];
}
}
}
if (ok) {
cout << "1\n";
continue;
}
for (auto id : esp) {
// retiramos e vermos se eh possivel
for (int i = 0; i < 30; i++) {
if ((1 << i) & a[id]) sum[i]--;
for (int j = i; j < 30; j++) {
f[i][j] -= freq[id][i][j];
}
}
}
int esq = 30, dir = -1;
for (int j = 0; j <= mx; j++) if (!sum[j]) {
esq = min (esq, j);
dir = max (dir, j);
}
if (f[esq][dir]) {
ok = true;
}
if (ok) {
cout << "1\n";
continue;
}
cout << "2\n";
}
}
signed main () {
ios::sync_with_stdio(0);cin.tie(0);
int t; cin >> t;
while (t--)
solve ();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3500kb
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: 875ms
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: 0
Accepted
time: 747ms
memory: 3544kb
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: 755ms
memory: 3600kb
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 3718th numbers differ - expected: '2', found: '1'